如何访问在加载window.load
& document.ready
函数后加载的HTML页面中的元素?在window.load中加载的dom中访问元素
我无法使用jQuery文档准备好或加载来访问它们。
如何访问在加载window.load
& document.ready
函数后加载的HTML页面中的元素?在window.load中加载的dom中访问元素
我无法使用jQuery文档准备好或加载来访问它们。
如果有任何事件可以在您需要访问它们或到达时使用,请使用该事件。
如果没有事件中,你可以挂接到,你可以等待他们使用计时器循环(例如,为他们投票),以显示:
(function() {
function doSomething() {
var elements = $("selector-for-the-elements");
if (!elements.length) {
// Not there yet, keep waiting
setTimeout(doSomething, 100); // 100ms = 1/10th second, adjust as needed
return;
}
// We have them, use them
}
$(doSomething); // Make the first call happen on document ready
})();
假设你使用触发器来访问元素时,下面的工作:
$("<trigger>").on("click", function() {
<access loaded element>
});
$(".parent_class").on("click",".the_element_you_need",function() {
//code here
});
是否有加载事件?点击事件不在页面 – user3555971
$(document).on("click",".your_element",function() {
//do something
});
既然你已经提到jQuery的,我给你一个jQuery明确的答案:
$("body").on("click",".selector-late",function(e){
//Your Code Here.
//e.delegateTarget returns the body element.
//e.currentTarget returns .selector-late element.
//e.target returns the element which was clicked. (could be a child of .selector-late too)
});
说明:
body
在这种情况下,它可能是元素的任何父项。)click
是我附加的事件。你可以根据需要改变。.selector-late
将成为您的元素的选择器,可能在将来随时加载。签名
$(staticAncestors).on(eventName, dynamicChild, function() {});
这种方法在detail here.
@ user3555971 - 有什么好运? –
解释与解释example..If的可能,请创建一个小提琴 – user3386779
你说的是通过访问JS动态HTML元素? – Sarath