问题
$(this).closest(".fieldfilters");
This returns nothing for me. The HTML structure is like this:
<div class="fieldfilters" >
<div class="filtri_ul_list">
<ul>
<li> <a></a></li>
</ul>
</div>
</div>
$(this)
is the <a>
. As far as I understand closest
traverses the DOM up and finds the closest match. Is there a problem with the selector being a class? Why doesn't this work?
回答1:
Your usage of .closest() is perfectly fine.
$(this).closest(".fieldfilters");
The most probable cause of your problem is that $(this) is not what you think it is. Check your context to see what 'this' really is.
回答2:
$(this).closest('div').find('.fieldfilters');
回答3:
This will get you the first occurrence.
$(".fieldfilters:first")
来源:https://stackoverflow.com/questions/15521678/using-jquery-closest-method-with-class-selector