为什么需要点击两次按钮才能使jQuery代码工作? 浏览器加载此文件,并加载脚本标记存在于头部分。但是这个代码只能在2次点击后才能使用。JavaScript动态加载jQuery库
function j1() {
$("button").click(function() {
$("p").hide();
});
}
function load1() {
var target = document.getElementsByTagName("head");
var newScript = document.createElement("script");
var url =
"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js";
newScript.src = url;
target[0].appendChild(newScript);
newScript = document.createElement("script");
url = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js";
newScript.src = url;
target[0].insertBefore(newScript, target[0].firstChild);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
</head>
<body onload="load1();">
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button onclick="j1();">Click me</button>
</body>
</html>
难道您调试码?在j1()的第一行设置一个断点。 –
我没有JS的调试器,但我非常彻底地检查它。我也在运行它,并用Chrome“检查”来检查它。 –
我能够动态地将JQuery添加到
或HTML(在浏览器上查看),但需要两次调用J1()才能让JQuery完成这项工作。顺便说一句,在load1()内部调用J1()也不起作用。 –