Using jQuery closest() method with class selector

試著忘記壹切 提交于 2019-12-10 23:18:50

问题


$(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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!