Using $(this) with multiple selectors, can you refer to one and not the other?
Let's say you have two selectors like this:
$(".class1, .class2").click(function() {
$(this).hide();
});
It seems that $(this)
refers to both selectors. Is there a way to only refer to one and not the other? (I dont mean just using .class1
or .class2
because I want it to refer to the element that is being clicked if there are multiple elements with those classes.)
I just realized that you could w开发者_开发技巧rite out the same thing twice, but that's not very nice:
$(".class1").click(function() {
$(this).hide();
$(.element).show();
});
$(".class2").click(function() {
$(.element).show();
});
No, in here:
$(".class1, .class2").click(function() {
$(this).hide();
});
this
in the callback function refers only to the single element that was clicked.
Click on the button and see for yourself: http://jsfiddle.net/ambiguous/GyEck/
if ($(this).is(".class2"))
精彩评论