jQuery - 查找没有事件的动态创建的元素 [英] jQuery - Find dynamically created element without events

查看:141
本文介绍了jQuery - 查找没有事件的动态创建的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

之前已经问过这个问题,答案是:

Tho this question has been asked before, and the answer is this:

$('#container').on('click','#dynamicElement', function(){ /* the code */ });

上面的代码将找到 #dynamicElement 当它被点击时。
但是如果没有点击,也没有任何其他事件怎么办?

The code above will find the #dynamicElement when its being clicked on. But what if there is no click, nor any other event?

假设以下情况:

$.ajax(
    url:'file.php',
    data: {'param':'value'},
    success: function(response){
         /*
         how would I get #dynamicElement if it was not click on?
         the element had no event fired at all, nor had any of its parennt
         containers.

          Now what?
         */
    }
);


推荐答案

如果你的新元素被添加到里面的页面成功回调,
此时您可以调用 $('#dynamicElement')

If your new element is being added to the page inside the success callback, at that point you can call $('#dynamicElement')

使用 $('#dynamicElement')回调之外的任何地方都不会返回该元素,因为它尚未添加到DOM还没有。

Using $('#dynamicElement') anywhere outside of the callback would not return the element since it had not been added to the DOM yet.

这篇关于jQuery - 查找没有事件的动态创建的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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