在我的html中说我有以下锚标签:
<a href="#fragment1">
link 1
</a>
<a href="http://someExternalResource.com">
link 2
</a>
<a href="#fragment2">
link 3
</a>
我将传递哪种选择器字符串document.querySelectorAll
以根据链接 1 和 3 是内部锚点片段链接的想法来选择它们。或者,针对这些目标的任何其他解决方案都会有所帮助。
以下内容将选择具有以 # 开头的 href 属性的所有元素。document.querySelectorAll('[href^="#"]')
我希望这有所帮助。
其他替代方法是将jQuery与正则表达式一起使用,用于特定的锚点href。 有关详细信息,请参阅此链接。
$('a:regex(href, ^#fragment.*)');