如何捕获所有子元素中的单击?

时间:2013-03-28 10:29:27

标签: jquery

问题很简单,但我做不到......在这里我的代码:

jQuery('nav').on('click', 'a',  function(event){
    console.log(jQuery(this));
});

2 个答案:

答案 0 :(得分:1)

nav必须是类或ID,导航似乎不是标准标记

如果是类名

jQuery('.nav').on('click', 'a',  function(event){
    console.log(jQuery(this));
});

如果是Id

jQuery('#nav').on('click', 'a',  function(event){
    console.log(jQuery(this));
});

这里有现场演示:http://jsfiddle.net/netme/hx8HR/

答案 1 :(得分:1)

你的代码看起来很好....但我认为你错过了document.ready函数

试试这个

jQuery(function(){  //ready function
  jQuery('nav').on('click', 'a',  function(event){
    console.log(jQuery(this));
  });
});