js中函数的一些”坑“

(1)点击事件写法如下,这里会先显示文本,点击之后弹出对话框。


<html>
    <head>
        <meta charset="utf-8" />
        <title>test for functiontitle>
    head>
    <body>
        <p id="Burning">My name is Burningp>
        <script type="text/javascript">
        window.onload = function(){
            var bur = document.getElementById("Burning");
            bur.onclick = Hello;

            function Hello(){


                alert("Hello world");
            }
        }   
        script>
    body>
html>

(2)这样些会首先弹出对话框,然后显示文本。这个的原因百度了一下,没懂!


<html>
    <head>
        <meta charset="utf-8" />
        <title>test for functiontitle>
    head>
    <body>
        <p id="Burning">My name is Burningp>
        <script type="text/javascript">
        window.onload = function(){
            var bur = document.getElementById("Burning");
            bur.onclick = Hello();

            function Hello(){


                alert("Hello world");
            }
        }   
        script>
    body>
html>

(4)这里会先显示文本,点击之后弹出对话框。


<html>
    <head>
        <meta charset="utf-8" />
        <title>test for functiontitle>
    head>
    <body>
        <p id="Burning" onclick="Hello()">My name is Burningp>
        <script type="text/javascript">

            //var bur = document.getElementById("Burning");
            //bur.onclick = Hello();
            /*if (typeof Hello == "function"){
                console.log("right");
            }*/
            function Hello(){


                alert("Hello world");
            }

        script>
    body>
html>

(5)只显示文本,点击事件不好使。


<html>
    <head>
        <meta charset="utf-8" />
        <title>test for functiontitle>
    head>
    <body>
        <p id="Burning" onclick="Hello">My name is Burningp>
        <script type="text/javascript">

            //var bur = document.getElementById("Burning");
            //bur.onclick = Hello();
            /*if (typeof Hello == "function"){
                console.log("right");
            }*/
            function Hello(){


                alert("Hello world");
            }

        script>
    body>
html>

(6)同(1)


<html>
    <head>
        <meta charset="utf-8" />
        <title>test for functiontitle>
    head>
    <body>
        <p id="Burning">My name is Burningp>
        <script type="text/javascript">
        window.onload = function(){
            var bur = document.getElementById("Burning");
            console.log(bur);
            bur.onclick = function(){
            /*if (typeof Hello == "function"){
                console.log("right");
            }*/


                alert("Hello world");
            };
        }

        script>
    body>
html>

(7)同(2)


<html>
    <head>
        <meta charset="utf-8" />
        <title>test for functiontitle>
    head>
    <body>
        <p id="Burning">My name is Burningp>
        <script type="text/javascript">
        window.onload = function(){
            var bur = document.getElementById("Burning");
            console.log(bur);
            bur.onclick = function(){
            /*if (typeof Hello == "function"){
                console.log("right");
            }*/


                alert("Hello world");
            }();
        }

        script>
    body>
html>

一段小代码:


<html>
    <head>
        <meta charset="utf-8" />
        <title>test for functiontitle>
    head>
    <body>
        <p id="Burning">My name is Burningp>
        <script type="text/javascript">
        window.onload = function(){
            var bur = document.getElementById("Burning");
            console.log(bur);
            //bur.onclick = Hello;
            if (typeof Hello() == 'function')
            {
                console.log("Hello world");
            }//typeof Hello()会执行Hello程序
            function Hello(){
                alert("Hello world");
            }
        }

        script>
    body>
html

你可能感兴趣的:(javascript,java,web开发)