<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<p>
1.JavaScript能直接写入Html输出流中
</p>
<script>
document.write("<h1>标题</h1>");
document.write("<p>段落</p>");
</script>
<p>
2.对事件的反应
</p>
<button type="button" onclick="alert('欢迎')">点我</button>
<!--//onclick事件 alert函数-->
<p id="demo">
3.改变HTML元素中的内容
</p>
<script>
function myFunction()
{
x=document.getElementById("demo");
x.innerHTML="3.新的内容";
}
</script>
<button type="button" onclick="myFunction()">点我修改</button>
<p>
4.改变Html图像的来源
</p>
<script>
function ChangeImage()
{
element=document.getElementById("myimage");
if(element.src.match("bulbon"))
{element.src="images/pic_bulboff.gif";}
else
{element.src="images/pic_bulbon.gif";}
}
</script>
<img id="myimage" onclick="ChangeImage()" src="images/pic_bulboff.gif" width="100" height="180">
<p>
点击灯泡可以打开关闭这盏灯
</p>
<p id="demo2">
5.改变HTML元素的样式
</p>
<script>
function myFunction2()
{
x=document.getElementById("demo2");
x.style.color="#ff0000";
}
</script>
<button type="button" onclick="myFunction2()">点我修改</button>
<p>
6.验证用户输入
</p>
<input id="demo3" type="text">
<script>
function myFunction3()
{
var x=document.getElementById("demo3").value;
if(x==""||isNaN(x))
{alert("不是数字")}
else
{alert("输入合法")}
}
</script>
<button type="button" onclick="myFunction3()">点击这里输入数字</button>
</body>
</html>