【发布时间】:2019-02-27 23:22:00
【问题描述】:
如果链接属于不同的主机名或不同的子域,我需要一种在新选项卡中打开链接的方法。有可能同一站点可以托管在不同的域或子域中。
我无法控制 html,因为这是由某种机器人获取的,我需要对其进行处理。
例 1。 页面请求网址:example.com
有链接:
<a href="//app.example.com">app example</a>
<a href="//www.info.example.com">info example</a>
<a href="https://example.com/abc">example abc</a>
<a href="https://www.example2.com">example2</a>
我需要它来解决
<a href="//app.example.com" target="_blank">app example</a>
<a href="//info.example.com" target="_blank">info example</a>
<a href="https://example.com/abc" target="_self">example abc</a>
<a href="https://www.example2.com" target="_blank">example2</a>
例 2。 **页面请求网址:info.example.com**
有链接:
<a href="//app.example.com">app example</a>
<a href="//www.info.example.com">info example</a>
<a href="http://tst.example.com/abc">example abc</a>
<a href="https//dev.example2.com">example2</a>
我需要它来解决
<a href="//app.example.com" target="_blank">app example</a>
<a href="//wwww.info.example.com" target="_self">info abc</a>
<a href="http://tst.example.com/abc" target="_blank">example abc</a>
<a href="https//dev.example2.com" target="_blank">example2</a>
我也试过这个 Open all external links open in a new tab apart from a domain
但它不适用于子域。
【问题讨论】:
-
你能分享你更新的js代码吗?提供的链接中的问题恰恰相反(但应该适用于您的场景)并且答案的作用非常不同。
-
嗨,@freedomn-m 我已经更新了我的问题,现在应该澄清更多。
标签: javascript jquery html anchor href