Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jQuery/JavaScript在脚本标记中不起作用_Javascript_Jquery_Html - Fatal编程技术网

jQuery/JavaScript在脚本标记中不起作用

jQuery/JavaScript在脚本标记中不起作用,javascript,jquery,html,Javascript,Jquery,Html,我想,我的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

我想,我的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>

$(文档).ready(函数(){
$(“#d4”)。单击(函数(){
var out=数学随机(1,4);
警惕(出去);
};
});

滚动d4


缺少click函数的右括号。

脚本标记不能有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>

$(文档).ready(函数(){
$(“#d4”)。单击(函数(){
var out=数学随机(1,4);
警惕(出去);
})这里
});
src

此属性指定外部脚本的URI;这可能是 用作直接在脚本中嵌入脚本的替代方法 文件。不应使用指定了src属性的脚本元素 在其标记中嵌入脚本


发出警报后,需要关闭click函数的括号

$(document).ready(function(){
    $("#d4").click(function(){
        var out = Math.random(1,4);
        alert(out);
    }); // <-- here
});
$(文档).ready(函数(){
$(“#d4”)。单击(函数(){
var out=数学随机(1,4);
警惕(出去);

});//工作情况如何?将document.ready代码放在jquery脚本标记之后的单独脚本标记中。