您使用什麼選擇器來引用on
函數中的孩子?示例如下。
$("#parent").on("click", $(this).find(".child"), function() {
console.log($(this));
});
在控制檯中引用的$(this)
是父級。我如何選擇孩子?注意:在父母下面的很多孩子,我只想得到$(this)
作爲孩子被點擊。
您使用什麼選擇器來引用on
函數中的孩子?示例如下。
$("#parent").on("click", $(this).find(".child"), function() {
console.log($(this));
});
在控制檯中引用的$(this)
是父級。我如何選擇孩子?注意:在父母下面的很多孩子,我只想得到$(this)
作爲孩子被點擊。
正確的方法是:
$("#parent").on("click", ".child", function() {
console.log($(this)) // this here refers to '.child' being clicked.
});
選擇
類型:String
選擇器串來過濾所選擇的元件的後代 這將ca處理程序。如果選擇器爲空或省略,則在處理器到達選定元素時總是調用該處理器。
代替父節點上註冊的處理程序,可以將其對孩子登記,以及:
$("#parent").children(".child").on("click", function() {
// fires when any child of #parent is clicked; $(this) will be the child node clicked
});
BTW,第二個參數'on'應該是一種選擇,而不是一個jQuery目的。 – SLaks 2013-05-12 14:59:09
.child不是#parent的直接子,所以我選擇了一個選擇器。如果我將它綁定到.child的直接父項,$(this)會引用child而不是父項? – 2013-05-12 15:27:19