0

我已经使用了两个 div 并使用 jquery 1.7.2 中的委托为其内部元素定义了单击事件。第一次点击事件按预期工作正常,第二次不工作。

 $("#first").delegate(".ask div:first","click",function(e){
   alert("hi"); 
 });

 $("#second").delegate(".ask div:first","click",function(e){
   alert("hello");
 });​

html标记:

<div id="first" >
<div class="ask"> 
    <div> hi  </div>
    <div> disabled </div>
</div>

</div>

<div id="second" >
  <div class="ask"> 
     <div> hello </div>
     <div> disabled </div>
</div>

http://jsfiddle.net/YjK3N/3/ ​ 但是在 jquery 低版本中,上面的代码可以正常工作。

4

2 回答 2

1

我已经尝试过了,你是对的,它不起作用。这段代码虽然有效:

$("#first").on("click",".ask div:nth-child(1)",function(e){
  alert("hi");
});

$("#second").on("click",".ask div:nth-child(1)",function(e){
  alert("hello");
});​
于 2012-10-05T06:20:58.050 回答
1

Use :first-child, it works

$("#first").delegate(".ask div:first-child","click",function(e){
  alert("hi");
});


$("#second").delegate(".ask div:first-child","click",function(e){
  alert("hello");
}) ;​

And it works with .on

于 2012-10-05T06:26:16.660 回答