JS innerHTML属性后jQuery函数不起作用 [英] jQuery functions not working after JS innerHTML property

查看:83
本文介绍了JS innerHTML属性后jQuery函数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在应用程序中使用以下代码:

I am using this code in my application:

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

#inventory_box元素已经存在,因此它只是将IMG喷出到#inventory_box.

The #inventory_box element is present already, so it just spews out the IMG into #inventory_box.

我想要的是能够单击ID名称为test的出现的IMG, 但不会像这样工作:

What I want is to be able to click this appeared IMG with id name test, but won't work like:

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

推荐答案

尝试一下,

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

阅读 on()

这篇关于JS innerHTML属性后jQuery函数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆