1. JavaScript:写入 HTML 输出
document.write("<h1>This is a heading</h1>");
2. JavaScript:对事件作出反应
<button type="button" onclick="alert('Welcome!')">点击这里</button>
3. JavaScript:改变 HTML 内容
x=document.getElementById("demo") //查找元素
x.innerHTML="Hello JavaScript"; //改变内容
4. JavaScript:改变 HTML 图像
<!DOCTYPE html>
<html>
<body>
<script>
function changeImage()
{
element=document.getElementById('myimage')
if (element.src.match("bulbon"))
{
element.src="/i/eg_bulboff.gif";
}
else
{
element.src="/i/eg_bulbon.gif";
}
}
</script>
<img id="myimage" onclick="changeImage()" src="/i/eg_bulboff.gif">
<p>点击灯泡来点亮或熄灭这盏灯</p>
</body>
</html>
5. JavaScript:改变 HTML 样式
x=document.getElementById("demo") //找到元素
x.style.color="#ff0000"; //改变样式
6. JavaScript:验证输入
if isNaN(x) {alert("Not Numeric")};
7. JavaScript 对大小写敏感。
函数 getElementById 与 getElementbyID 是不同的。
同样,变量 myVariable 与 MyVariable 也是不同的。
8. 对代码行进行折行
可以在文本字符串中使用反斜杠对代码行进行换行。下面的例子会正确地显示:
document.write("Hello \
World!");
不过,您不能像这样折行:
document.write \
("Hello World!");
本文介绍JavaScript在网页开发中的核心应用,包括写入HTML输出、响应事件、改变HTML内容及图像、修改样式等基本操作,并强调JavaScript对大小写的敏感性。
1595

被折叠的 条评论
为什么被折叠?



