admin管理员组文章数量:1026989
Assume I select an element using $(mySelector)
. I would like to select the closest heading to it, so if the closest heading element to it was an <h2>
, it would select that, but if the closest was an <h3>
, it would select that instead. How can I do this?
Assume I select an element using $(mySelector)
. I would like to select the closest heading to it, so if the closest heading element to it was an <h2>
, it would select that, but if the closest was an <h3>
, it would select that instead. How can I do this?
-
I tried
closest()
, but of course that doesn't tell me just how close it is, so I can't tell it to choose the closest "h", using a switch/if...else if. – Bluefire Commented Sep 21, 2012 at 18:01 - What is your selector? A child of h2/h3 or a sibling? – Ram Commented Sep 21, 2012 at 18:02
- A child, i.e. it is within the heading. – Bluefire Commented Sep 21, 2012 at 18:04
1 Answer
Reset to default 8The ma in selectors means "or". So you may do this :
$(mySelector).closest('h3, h2')
This will return 0 or 1 element, the closest if more than one match.
Assume I select an element using $(mySelector)
. I would like to select the closest heading to it, so if the closest heading element to it was an <h2>
, it would select that, but if the closest was an <h3>
, it would select that instead. How can I do this?
Assume I select an element using $(mySelector)
. I would like to select the closest heading to it, so if the closest heading element to it was an <h2>
, it would select that, but if the closest was an <h3>
, it would select that instead. How can I do this?
-
I tried
closest()
, but of course that doesn't tell me just how close it is, so I can't tell it to choose the closest "h", using a switch/if...else if. – Bluefire Commented Sep 21, 2012 at 18:01 - What is your selector? A child of h2/h3 or a sibling? – Ram Commented Sep 21, 2012 at 18:02
- A child, i.e. it is within the heading. – Bluefire Commented Sep 21, 2012 at 18:04
1 Answer
Reset to default 8The ma in selectors means "or". So you may do this :
$(mySelector).closest('h3, h2')
This will return 0 or 1 element, the closest if more than one match.
本文标签:
版权声明:本文标题:javascript - Select closest element of type "h", e.g. h1, h2, etc - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1744972636a2127227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论