如何提高事件点击量,将字段添加到 Ajax

How to raise an event click, have fields added to Ajax?

本文关键字:字段 添加 Ajax 何提高 事件      更新时间:2023-09-26

我有正常工作的代码

$(document).on('click',"a.img,a.imgs",function() {
    $(this).next().find('a:first').click();
    return false;
});

但是当我添加新字段 ajax 时(例如显示更多),那么对于它们,这段代码不起作用,这很可悲

编辑

了我的答案,因为我误读了你的代码并混淆了一切。

以下是另一个 SO 线程的解释,可以帮助您解决问题:

由于以下原因之一,它可能不起作用:

  • 不使用最新版本的 jQuery
  • 没有准备好将代码包装在 DOM 中
  • 或者您正在执行某些操作,导致事件不会冒泡到文档上的侦听器。
$(document).ready(function() {
    // This WILL work because we are listening on the 'document', 
    // for a click on an element with an ID of #test-element
    $(document).on("click","#test-element",function() {
        alert("click bound to document listening for #test-element");
    });
    // This will NOT work because there is no '#test-element' ... yet
    $("#test-element").on("click",function() {
        alert("click bound directly to #test-element");
    });
    // Create the dynamic element '#test-element'
    $('body').append('<div id="test-element">Click mee</div>');
});

$(document).on("click"...不工作?