2010-05-05 17 views
0

我下面W3Schools的網站上的JavaScript教程工作,我有以下代碼:W3Schools的教程例如不與科達在Mac

<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> 

,每當我預覽在科達或Safari中的警報從未表演向上。

在此先感謝!

+2

偏題:W3Schools經常過時或表現不佳。爲了更好的替代品,嘗試在http://stackoverflow.com/questions/646032/whats-the-best-javascript-tutorial – outis 2010-05-05 00:16:10

+3

http://w3fools.com/ – 2011-01-14 17:59:48

回答

3

「功能confirmShow」=> 「功能confirmShow()」

螢火蟲是良好的JS調試,嘗試。 Safari也有選項,AFAIK。

+0

好的,謝謝。我不知道爲什麼我沒有看到這個:P – 2010-05-05 00:28:18

+2

洛爾我甚至沒有注意到他已經離開了括號... – Robusto 2010-05-05 00:31:15

0

我不知道這是否是您的問題,但您的按鈕是以外的<body>標籤。這可能會引起你一些麻煩......

另外一個通常會放一個這樣的腳本在<head>元素。只是FYI。

+0

哦建議,只注意到。我把按鈕放回到車身標籤中,它仍然不起作用。 – 2010-05-05 00:13:54

0

1)W3Schools的填充有錯誤和遺漏。更好的教程可以在howtocreate.co.uk

2)找到你沒有DOCTYPE聲明,並且您使用XHTML語法。

2.1)IE不支持true,請參閱webdevout.net/articles/beware-of-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> 
+0

好吧,我不認爲我是用XHTML語法教程上HTML,而不是XHTML。嗯... – 2010-05-05 01:22:07

1

功能confirmShow {

功能confirmShow() { ?