2014-02-23 78 views
1

我想要我的HTML文檔輸出龍與地下城的隨機數。但是,代碼無法正常工作,我無法解決原因。jQuery/JavaScript腳本標記不工作

<!DOCTYPE html> 
<html> 
<head> 
<script src="//code.jquery.com/jquery-1.10.2.js"> 
$(document).ready(function(){ 
    $("#d4").click(function(){ 
    var out = Math.random(1,4); 
    alert(out); 
    }; 
}); 
</script> 
</head> 
<body> 
    <p id ="d4">Roll a d4</p> 
</body> 
</html> 
+0

它是如何工作的? – Yatrix

+1

將您的document.ready代碼放在jquery腳本標記後的單獨腳本標記中。 – WebServer

回答

8

腳本標籤不能有src屬性和身體,你需要使用單獨的腳本標籤

<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script> 
$(document).ready(function() { 
    $("#d4").click(function() { 
     var out = Math.random(1, 4); 
     alert(out); 
    }); //missing) here 
}); 
</script> 

Script SRC

該屬性指定外部腳本的URI;可以使用 作爲將腳本直接嵌入到 文檔中的替代方法。具有指定的src屬性的腳本元素不應該 在其標籤中嵌入腳本。

0

缺少點擊函數的右括號。

1

提醒後,您需要關閉點擊功能的括號。

$(document).ready(function(){ 
    $("#d4").click(function(){ 
     var out = Math.random(1,4); 
     alert(out); 
    }); // <-- here 
});