简体   繁体   English

为什么我不能使用jQuery.click()定位li元素?

[英]Why can't I target li element with jQuery.click()?

I have this html: 我有这个HTML:

<ul id="ul_places_list"> 
  <li data-placesCode="6">  
    <a class="places_dtl" href="#">  
      <div>  
        <img src="http://dev.mysite.it/images/9_1418893365.jpg">  
      </div>  
      <label>Coming Out</label>  
    </a>   
  </li>
  <li data-placesCode="8">  
    <a class="places_dtl" href="#">  
      <div>  
        <img src="http://dev.mysite.it/images/9_1418893594.jpg">  
      </div>  
      <label>Friends</label>  
    </a>   
  </li>
</ul> 

and this javascript 和这个JavaScript

$(document).delegate('a','click',function(){//used to switch page
    console.log('delegate executed');
    var a = $(this);
    if(a.attr('href') != '#'){
        event.preventDefault();
        toPage(a.attr('href'));
    }
});
$(document).ready(function(){
    $('#ul_places_list li').click(function(){
        var code = $(this).attr('data-placesCode');
        console.log('code is: ' +code);
    });
});

The problem is that when I click on list element only the delegated click on the element fires and not the event on the "li" element. 问题是,当我单击列表元素时,仅会激发该元素上的委派单击,而不会触发“ li”元素上的事件。 The "li" elements are added after an Ajax call. 在Ajax调用之后添加“ li”元素。
What's going? 怎么了 What am I missing? 我想念什么?

This )}; 这个)}; was the problem. 是问题所在。 Should be this }); 应该是}); .

  $(document).delegate('a','click',function(){//used to switch page console.log('delegate executed'); var a = $(this); if(a.attr('href') != '#'){ event.preventDefault(); toPage(a.attr('href')); } }); $(document).ready(function(){ $('#ul_places_list li').click(function(){ var code = $(this).attr('data-placesCode'); console.log('code is: ' +code); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <ul id="ul_places_list"> <li data-placesCode="6"> <a class="places_dtl" href="#"> <div> <img src="http://dev.mysite.it/images/9_1418893365.jpg"> </div> <label>Coming Out</label> </a> </li> <li data-placesCode="8"> <a class="places_dtl" href="#"> <div> <img src="http://dev.mysite.it/images/9_1418893594.jpg"> </div> <label>Friends</label> </a> </li> </ul> 

The below answer actually is just a different implementation of your code. 下面的答案实际上只是代码的另一种实现。

The real answer is 真正的答案是

This )}; 这个)}; was the problem. 是问题所在。 Should be this }); 应该是}); .

From the other answer: https://stackoverflow.com/a/27571915/561731 从另一个答案: https : //stackoverflow.com/a/27571915/561731

But you really should be using .on in new code :-) 但是,您确实应该在新代码中使用.on :-)


Old answer: 旧答案:

Try using .on 尝试使用.on

So you can do: 因此,您可以执行以下操作:

$(document).on('click', 'a', function () {
    //event for anchor tag
});

$(function () {
    $('#ul_places_list').on('click', 'li', function () {
        //stuff for li click event
   });
});

if you add the "li" elements in a AJAX call you should add the click event after the elements load. 如果您在AJAX调用中添加“ li”元素,则应在元素加载后添加click事件。 Add this when ajax call end: 在ajax调用结束时添加以下内容:

Sys.WebForms.PageRequestManager.getInstance().add_endRequest(addEventToControls)

function addEventToControls()
{
 $('#ul_places_list li').click(function(){
            var code = $(this).attr('data-placesCode');
            console.log('code is: ' +code);
        });
}

The add_endRequest Manager fire the handler when a Ajax Call end. 当Ajax调用结束时,add_endRequest管理器将触发处理程序。

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

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