jQuery add target =" _blank"用于传出链接 [英] jQuery add target="_blank" for outgoing link
本文介绍了jQuery add target =" _blank"用于传出链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要一些帮助来创建jquery脚本:)
我的HTML上有一些这样的链接。
I need some help to create jquery script :)
I have some of link like this on my HTML.
<a href="http://google.com">Google</a>
<a href="/">Home</a>
<a href="http://www.gusdecool.com/">Home</a>
<a href="contactus.html">Contact Us</a>
现在我希望jQuery检查我页面上的所有链接。如果该链接在我的服务器之外(我的服务器是 gusdecool.com
)。然后添加 target =_ blank
。结果将是这样的
And now i want jQuery to check all of the link on my page. if that link is outside of my server (my server is gusdecool.com
). Then add target="_blank"
. and the result will be like this
<a href="http://google.com" target="_blank">Google</a>
<a href="/">Home</a>
<a href="http://www.gusdecool.com/">Home</a>
<a href="contactus.html">Contact Us</a>
推荐答案
假设所有外部链接都以开头http:// 你可以这样做:
assuming that all external links will start with http:// you could do this:
$('a [href ^ =http://]' ).not('a [href * = gusdecool]')。attr('target','_ blank');
这篇关于jQuery add target =" _blank"用于传出链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文