jQuery/JavaScript无法在脚本标记内工作

Ros*_*son 1 html javascript jquery

我想,我的HTML文档输出龙与地下城的随机数.但是,代码不起作用,我无法锻炼为什么.

<!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>
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 9

脚本标记不能具有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>
Run Code Online (Sandbox Code Playgroud)

脚本 src

该属性指定外部脚本的URI; 这可以用作直接在文档中嵌入脚本的替代方法.指定了src属性的脚本元素不应在其标记中嵌入脚本.