javascript - jQuery 函数在 JS innerHTML 属性之后不起作用

标签 javascript jquery innerhtml craftyjs

我在我的应用程序中使用这段代码:

document.getElementById("inventory_box").innerHTML = "<img src='./img/rock.gif' id='test' />";

#inventory_box 元素已经存在,所以它只是将 IMG 喷出到 #inventory_box 中。

我想要的是能够点击这个出现的 IMG id name test, 但不会像这样工作:

$("#test").click(function() {
                // Run this
            });

最佳答案

试试这个,

$(document).on('click',"#test",function() {
    alert('test');
});

阅读on()

关于javascript - jQuery 函数在 JS innerHTML 属性之后不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19224085/

相关文章:

javascript - 如何在不使用 innerHTML 的情况下在 JavaScript 中添加不间断空格?

javascript - 选择 HTML 选择/下拉列表中的项目时会触发什么事件?

javascript - 在 <p> 中设置文本时无法呈现 <br>

javascript - 错误 TS7017 : Index signature of object type implicitly has an 'any' type in form validation angular 2

javascript - JQuery 获取元素的值 : onfocus ="inputInlineHint(self);"

javascript - 将 HTML 实体分配给 innerHTML 时出现 DOM 异常

javascript - 在括号内使用点会破坏 swig 模板

php - 用于显示数据的 Mysql 查询

javascript - html2canvas : remove empty space above the picture

javascript - 无法读取未定义的属性 'innerHTML'