Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
假设我使用$(mySelector). 我想选择最接近它的标题,所以如果最接近它的标题元素是一个<h2>,它会选择那个,但如果最近的是一个<h3>,它会选择那个。我怎样才能做到这一点?
$(mySelector)
<h2>
<h3>
选择器中的逗号表示“或”。所以你可以这样做:
$(mySelector).closest('h3, h2')
这将返回 0 或 1 个元素,如果有多个匹配项,则最接近。