在 HTML4.01 可以使用 target=”_blank”, 但 XHTML1.0是不被允许的.
<a href="document.html" target="_blank">Open in New Window</a>
在 XHTML1.0 需要用 rel=”external” 属性.
<a href="document.html" rel="external">Open in New Window</a>
当然这是符合 XHTML 1.0 strict 标准规范, 而且你不要忘记在文件头声明文档类型:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
如果文档类型忘记了或者有的浏览器不支持(像老版本的IE), 那么就无法达到预期的目的, 不过可以通过以下 Javascript 使新标签一样可以按照老方式工作:
1 2 3 4 5 6 7 8 9 10 11 | // helloacm.com function externallinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i = 0; i < anchors.length; i++) { var anchor = anchors[i]; if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; } } window.onload = externallinks; |
// helloacm.com function externallinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i = 0; i < anchors.length; i++) { var anchor = anchors[i]; if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; } } window.onload = externallinks;
最好保存成一个文件, external.js, 然后通过以下方式调用 (最好放在 head 之间加载):
<script async type="text/javascript" src="external.js"></script>
async 指明了调用方式为异步, 这样就不会导致页面加载过慢.
英文同步: https://helloacm.com/html-hyper-link-in-new-windows-by-xhtml-1-0-standard/
强烈推荐
- 英国代购-畅购英伦
- TopCashBack 返现 (英国购物必备, 积少成多, 我2年来一共得了3000多英镑)
- Quidco 返现 (也是很不错的英国返现网站, 返现率高)
- 注册就送10美元, 免费使用2个月的 DigitalOcean 云主机(性价比超高, 每月只需5美元)
- 注册就送10美元, 免费使用4个月的 Vultr 云主机(性价比超高, 每月只需2.5美元)
- 注册就送10美元, 免费使用2个月的 阿里 云主机(性价比超高, 每月只需4.5美元)
- 注册就送20美元, 免费使用4个月的 Linode 云主机(性价比超高, 每月只需5美元) (折扣码: PodCastInit2022)
- PlusNet 英国光纤(超快, 超划算! 用户名 doctorlai)
- 刷了美国运通信用卡一年得到的积分 换了 485英镑
- 注册就送50英镑 – 英国最便宜最划算的电气提供商
- 能把比特币莱特币变现的银行卡! 不需要手续费就可以把虚拟货币法币兑换
微信公众号: 小赖子的英国生活和资讯 JustYYUK