<!DOCTYPE html>
<html>
<body>
<p>假设 y=5,计算 x=y+2,并显示结果。</p>
<button οnclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=y+2;
var demoP=document.getElementById("demo")
demoP.innerHTML="x=" + x;
}
</script>
</body>
<html>
<body>
<p>假设 y=5,计算 x=y+2,并显示结果。</p>
<button οnclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=y+2;
var demoP=document.getElementById("demo")
demoP.innerHTML="x=" + x;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<p>假设 y=5,计算 x=y+2,并显示结果。</p>
<button οnclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction()
{
var y=5;
var x=y+2;
document.getElementById("demo")innerHTML="x=" + x;
}
</script>
</body>
</html>
参考http://www.runoob.com/js/js-variables.html