使用 jQuery 获取所有 mailto 链接
我需要一种模式来遍历文档并获取 href 中包含 mailto 的所有链接:
<a href="mailto:[email protected]">text</a>
我当然可以轻松获取所有 a
元素 ($("a")
) 并检查每个 href
属性以查看它是否指向 mailto,但我认为 jQuery 具有某种形式的模式匹配,可以让我做到这一点。
实现这一目标的最佳方法是什么?
I need a pattern that will traverse the document and get me all links that have mailto in their href:
<a href="mailto:[email protected]">text</a>
I could of course easily get all a
elements ($("a")
) and check each href
attribute to see if it points to a mailto but I think that jQuery has some form of pattern matching that will allow me to do just that.
What is the best way to achieve that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
双引号通常是多余的,但在这种特殊情况下是必需的,因为
:
否则会被解释为伪选择器的开头。因此$('a[href^=mailto]')
也可以工作,但在这种特定情况下,引号可能是一种更简洁的方式。Double quotes are usually redundant, but needed in this special case, because
:
would otherwise be interpreted as the start of a pseudo-selector. So$('a[href^=mailto]')
would also work, but in this particular scenario, the quotes are probably a neater way to go.