假设我使用 jQuery 在我的 DOM 中插入任何内容,像这样:
$('#someselector').prepend('<img src="/img/myimage.gif" id="someid" />');
在 jQuery 中有没有一种方法可以获取引用这个新图像的 jQuery
对象,而不必像
var myImage = $('#someselector #someid');
???
最佳答案
在 之前将其变成 jQuery 对象:
var $img = $('<img src="/img/myimage.gif" id="someid" />');
$('#someselector').prepend($img);
$img.foo();
关于javascript - 获取 jQuery 插入的 DOM 元素,而无需在插入后重新查询元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5950318/