我正在尝试从字符串 html_doc
中提取 id=obj1
并尝试将 onclick 函数
附加到它
document.addEventListener("DOMContentLoaded",
function (event) {
$ajaxUtils.sendGetRequest("data/GOG123.json",
function (res) {
var residue=res.residues;
var html_doc = "<div>" + "<Button id = obj1>" + residue[0].index + "</Button> " + "</div>";
console.log(html_doc);
var html_doc2;
html_doc2= $("html_doc").find("obj1");
$(document).ready(function(){
html_doc2.click(function () {
alert("Hello!");
});
});
var div1=$("#infoDiv");
div1.append(html_doc);
}
);
});
它不工作(即不显示警告消息)也没有抛出任何错误。
有人可以帮我做同样的事情吗?
我提到了 Why is this jQuery click function not working?但它对我没有用。
最佳答案
你也可以使用.on()
点击事件。
$(document).on("click", "#obj1", function(){
alert("clicked");
});
关于javascript - 即使在使用 $(document).ready(function() {}) 之后,jquery click 函数也无法正常工作;甚至在使用 $(function) block 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54567318/