[英]filter() not working like I expect when using $(this)
Can someone explain why this works: 有人可以解释为什么这样起作用:
$(function(){
$("ol li a").click(function(){
$("ol li a").filter(":even").css("color", "orange");
});
});
But this doesn't: 但这不是:
$(function(){
$("ol li a").click(function(){
$(this).filter(":even").css("color", "orange");
});
});
I thought using $(this) in this context would refer to the jquery object which has my original selector in it. 我认为在这种情况下使用$(this)将引用其中具有我的原始选择器的jquery对象。
因为$(this)
是单个元素(事件的目标),而$("ol li a")
0到N个与该扇区匹配的元素。
this
is not collection of the elements that correspond to original selector, it is particular element that received the event. this
不是对应于原始选择器的元素的集合,而是接收事件的特定元素。 If you have selector that returns single element than $(this)
and $(selector)
are interchangeable, but not in your case. 如果您有返回单个元素的选择器,则
$(this)
和$(selector)
是可互换的,但不适用于您的情况。
当您单击按钮时,事件将触发并创建$(this)
,但是, $(this)
仅是被单击的元素,而不是整个元素集,因此将无法过滤,因为只有一个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.