【代码笔记】Web-JavaScript-JavaScript 变量

一,效果图。

二,代码。

复制代码
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>javascript 变量</title>
</head>

<body>
    <script>
    var x = 5;
    var y = 6;
    var z = x + y;

    document.write(x + "<br>");
    document.write(y + "<br");
    document.write(z = "<br>");
    </script>
    <script>
    var pi = 3.14;
    var name = "bill gate";
    var answer = "yes i am ";

    document.write(pi + "<br>");
    document.write(name + "<br>");
    document.write(answer + "<br>");
    </script>
    <p>点这里来创建变量,并显示结果</p>
    <button onclick="myFunction()">点击这里</button>
    <p id="demo"></p>
    <script>
    function myFunction() {
        var carname = "volvo";
        document.getElementById("demo").innerHTML = carname;
    }
    </script>
    <p>假设 y=5,计算 x=y+2,并显示结果</p>
    <button onclick="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>
复制代码

 

参考资料:《菜鸟教程》

posted @ 2018-11-28 09:25  花儿迎风笑  阅读(232)  评论(0编辑  收藏  举报