jQuery .on(“click”...),动态创建的元素不起作用

时间:2021-11-18 18:56:57

So I am having some troubling results with jQuery ".on" function and a dynamically created element, the code I am using is this:

所以我使用jQuery“.on”函数和动态创建的元素有一些令人不安的结果,我使用的代码是这样的:

this is called in a function sometime random during runtime:

这在运行时有时随机调用:

$(".activity-feed").append('<div class="feed-story big-feed-story feed-story-comment multiline-feed-story">\
        <div class="photo-view inbox-size photo-view-rounded-corners">\
            <div class="comment-icon">\
            </div>\
        </div>\
        <div class="comment-content">\
            <div class="delete ucomment-delete click-target" id="ucomment-'+uqid+'" tabindex="-1" style="outline:none;"></div>\
            <span class="feed-story-creator"><a href="/user/'+uid+'/'+username+'">'+username+'</a>&nbsp;</span>\
                <span class="comment-text">\
                    <span>\
                        <span style="white-space: pre-wrap;">'+$(".comment-box").html()+'</span>\
                    </span>\
                </span>\
            <div>\
                <span class="feed-story-footer"><span class="story-timestamp-view">'+time+'</span></span>\
            </div>\
        </div>\
    </div>');

and then this function should allow for comment deletion (its defined at the bottom of the page, not after document ready)

然后这个函数应该允许删除注释(它在页面底部定义,而不是在文档就绪后)

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

however, I click on the "ucomment-delete" button, and nothing happens :(, however if I click the comments that were there on load, it works
Why is this? What am I doing wrong?

然而,我点击“ucomment-delete”按钮,没有任何反应:(但是,如果我点击加载时出现的评论,它的作用为什么会这样?我做错了什么?

4 个解决方案

#1


10  

try event delegation

尝试事件委托

$(document).on("click", ".delete", function() {
 alert();
});

#2


1  

You need to bind it to an element that already exists on the page and then can specify a selector for elements that may not yet exist on the page:

您需要将其绑定到页面上已存在的元素,然后可以为页面上可能尚不存在的元素指定选择器:

$(".activity-feed").on("click",".delete", function() {
    alert('test....');
});

See this fiddle for an example:

看一下这个小提琴的例子:

http://jsfiddle.net/G4MGZ/

http://jsfiddle.net/G4MGZ/

#3


0  

if on is not working then your jquery version is older try delegate

如果on不工作那么你的jquery版本是较旧的尝试委托

$('body').delegate('.delete','click',function(){
 //stuff  
});

#4


0  

The .on() HAS to be wrapped in a document.ready(), even if it's at the end of the file. And I think what @John Koemer says is true, too.

.on()包含在document.ready()中,即使它位于文件的末尾。我认为@John Koemer所说的也是如此。

#1


10  

try event delegation

尝试事件委托

$(document).on("click", ".delete", function() {
 alert();
});

#2


1  

You need to bind it to an element that already exists on the page and then can specify a selector for elements that may not yet exist on the page:

您需要将其绑定到页面上已存在的元素,然后可以为页面上可能尚不存在的元素指定选择器:

$(".activity-feed").on("click",".delete", function() {
    alert('test....');
});

See this fiddle for an example:

看一下这个小提琴的例子:

http://jsfiddle.net/G4MGZ/

http://jsfiddle.net/G4MGZ/

#3


0  

if on is not working then your jquery version is older try delegate

如果on不工作那么你的jquery版本是较旧的尝试委托

$('body').delegate('.delete','click',function(){
 //stuff  
});

#4


0  

The .on() HAS to be wrapped in a document.ready(), even if it's at the end of the file. And I think what @John Koemer says is true, too.

.on()包含在document.ready()中,即使它位于文件的末尾。我认为@John Koemer所说的也是如此。