代码之家  ›  专栏  ›  技术社区  ›  Mark Szymanski

W3Schools教程示例不使用Mac上的Coda

  •  0
  • Mark Szymanski  · 技术社区  · 16 年前

    我正在W3Schools网站上学习javascript教程,我有以下代码:

    <html>
    
    <head>
    
    <title>Hello!</title>
    
    </head>
    
    <body>
    
    <script type="text/javascript">
    function confirmShow
    {
        var r = confirm("Press one...")
        if (r == true)
        {
            alert("Button pressed == OK")
        }
    
        if (r == false)
        {
            alert("Button pressed == Cancel")
        }
    }
    </script>
    <input type="button" onclick="confirmShow()" value="Show Confirm Box" />
    </body>
    
    
    
    </html>
    

    每当我在Coda或Safari中预览它,警报就不会出现。

    事先谢谢!

    4 回复  |  直到 14 年前
        1
  •  3
  •   Nikita Rybak    16 年前

    “函数confirmShow”=>“函数confirmShow()”

    Firebug很适合JS调试,试试看。Safari也有选择,Afaik。

        2
  •  1
  •   j2me    16 年前

    功能确认显示 {

    函数确认显示() { ?

        3
  •  0
  •   Robusto    16 年前

    我不知道这是不是你的问题,但你的按钮是 外部 这个 <body> 标签。可能会给你带来麻烦…

    另外,我们通常会在 <head> 元素。只是FIY。

        4
  •  0
  •   boogyman    16 年前

    1)W3学校存在错误和遗漏。在howtocreate.co.uk可以找到更好的教程。

    2)您没有doctype声明,并且使用的是xhtml语法。

    2.1)IE不支持true,有关详细信息,请参阅webdevout.net/articles/当心-xhtml 3)您需要按照规范将一个元素和另一个块级元素封装在一个元素中。

    有关正确的HTML5文档,请参见下文。注意位置和语法

    <!DOCTYPE html>
    <html>
    <head>
        <title>Hello!</title>
    <script>
    function confirmBox() {
        var ret = confirm('Some Text');
    /*
    Note the 3 equal signs. This is a strict comparison operator, to check both the 'value' as well as the type. see https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators for more
    */
        if(ret === true) { 
            alert('Alert box for "Okay" value');
        }
        else if(ret === false) {
            alert('Alert box for "Cancel" value');
        }
    }
    window.onload = function() {
        // Execute the confirmBox function once the 'button' is pressed.
        document.getElementById('confirmBox').onclick = confirmBox;
    }
    </script>
    </head>
    <body>
    
    
    <form>
    <p>
    <input type="button" id='confirmBox' value="Show Confirm Box">
    </p>
    </form>
    
    
    </body>
    </html>