2016-06-13 66 views

回答

0

如果有任何事件可以在您需要访问它们或到达时使用,请使用该事件。

如果没有事件中,你可以挂接到,你可以等待他们使用计时器循环(例如,为他们投票),以显示:

(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 
})(); 
0

假设你使用触发器来访问元素时,下面的工作:

$("<trigger>").on("click", function() { 
    <access loaded element> 
}); 
0
$(document).on("click",".your_element",function() { 
//do something 
}); 
1

既然你已经提到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) 
}); 

说明:

  1. 我们这是已经存在的元素上附加的事件侦听器。 (body在这种情况下,它可能是元素的任何父项。)
  2. click是我附加的事件。你可以根据需要改变。
  3. .selector-late将成为您的元素的选择器,可能在将来随时加载。

签名

$(staticAncestors).on(eventName, dynamicChild, function() {}); 

这种方法在detail here.

+0

@ user3555971 - 有什么好运? –