0

Im using

$(document).bind('click','.q',function(e){console.dir(e.target);});

my html is as so:

<div class="b"style="width:1000px;height:1000px">
    <div class="q"style="width:10px;height:10px"></div>
    <div class="q"style="width:10px;height:10px"></div>
    more .q added later...

</div>

but when I click anywhere on .b (before a .q is appended) i get my console giving me the dir for e.target of .b

why?

I am using this code because live() is now removed.

So my code would have been

$('.q').live('click',function(e){console.dir(e.target);});
4

2 回答 2

3

bind不支持委托事件,因此您的第二个参数是eventData.
您应该使用on而不是bind

// Not document for better performance.
$('div.b').on('click','.q', function(e){console.dir(e.target);});

您可以eventData使用: 来访问参数e.data,如果您对其进行控制台,您将".q"在控制台中看到。

于 2013-10-07T12:50:14.303 回答
1

绑定 语法中

$(document).bind('click',function(e){console.dir(e.target);});

存在所以改变

$(document).bind('click','.q',function(e){console.dir(e.target);});

$(document).on('click','.q',function(e){console.dir(e.target);});

引用On绑定

于 2013-10-07T12:49:14.570 回答