简体   繁体   English

单击问题:动态生成的链接未触发单击功能

[英]Click Issue: Dynamically Generated Links Not Triggering Click Function

Below are the two snippets of code, for some reason nothing is happening, yet other jQuery functions from the same JS file are executing fine on the page with the UL, is it something that's staring me in the face!? 下面是代码的两个片段,由于某种原因没有发生任何事情,但是同一个JS文件中的其他jQuery函数在UL页面上正常运行,是否正在盯着我看?

<ul id="activityPaganation" class="paganation">
    <li>1</li>
    <li><a href="#">2</a></li>
    <li><a href="#">3</a></li>
    <li><a href="#">4</a></li>
    <li><a href="#">5</a></li>
    <li><a href="#">6</a></li>
</ul>



$(document).ready(function() { 
    $('#activityPaganation li a').on("click", function(e) {
            e.preventDefault();     
            var pid = $(this).text();

            alert(pid);
    });
});

You can use event delegation, since this is dynamically added to DOM. 您可以使用事件委派,因为它会动态添加到DOM。

 $(document).on("click", "#activityPaganation li a", function(e) {
            e.preventDefault();     
            var pid = $(this).text();

            alert(pid);
    });

instead of document use some container that exists in DOM at any point in time. 而不是文档使用在任何时间点存在于DOM中的一些容器。

if the li's are added and your ul #activityPaganation exists at any time then: 如果添加了li并且您的ul #activityPaganation存在,那么:

  $('#activityPaganation').on("click", "li a", function(e) {
                e.preventDefault();     
                var pid = $(this).text();

                alert(pid);
        });

otherwise move your event binding to the load's complete call back. 否则将事件绑定移动到负载的complete回调。

ie

$('something').load('someurl', function(){
 $('#activityPaganation li a').on("click", function(e) {
            e.preventDefault();     
            var pid = $(this).text();

            alert(pid);
    });
});

Reason is that with direct event binding on the element it needs to exist in DOM, instead workaround is to delegate the event to a container that exists in DOM at any point or document head so that the when element is added in future it gets the event delegated from its parent. 原因是,对于元素的直接事件绑定,它需要存在于DOM中,而不是解决方法是将事件委托给任何点或文档头部中存在于DOM中的容器,以便将来添加when元素时获取事件从其父母那里委托。 Effectively it uses event bubbling for this effect. 实际上,它使用事件冒泡来实现此效果。

See 看到

Try event delegation instead 请尝试事件委派

Replace 更换

$('#activityPaganation li a').on("click", function(e) {

with

$(document).on("click", '#activityPaganation li a', function(e) {

Because the elements are being added dynamically to the page, the events are not bound to the elements that are added. 由于元素是动态添加到页面的,因此事件不会绑定到添加的元素。

The event signature that you are trying , will only bind the events to the elements taht are present in the DOM at that point of time. 您正在尝试的事件签名将仅将事件绑定到该时间点在DOM中存在的元素。 So delegating the event to the closest static ancestor will solve your problem as the event bubbles up and is handled by it. 因此,将事件委托给最近的静态祖先将解决您的问题,因为事件会冒泡并由其处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM