0

我有一个列表,我想为每个 li 附加一个单击,尽管我无法使其正常工作,例如下面的示例。请帮忙!?谢谢,

 <ul>
   <li>a</li>
   <li>b</li>
   <li>c</li>
   <li>d</li>
 </ul>


 $('li').each(function () {
     $(this).click(function (e) {
        // access the letter - not sure how to do this (?)
     });
  });
4

2 回答 2

1

做就是了:

$(function(){
     $('li').click(function (e) {
        alert(this.innerHTML);  // not sure how to access this (?)
     });
});

您不需要在li. 您只需要一个选择器,同时注册点击事件,然后使用普通元素属性this.innerHTML或 jquery 方法访问,$(this).text()或者$(this).html()

演示

于 2013-06-27T18:01:02.913 回答
0
$('li').click(function (e) {
    $(this).text(); 
 });
于 2013-06-27T18:04:00.270 回答