Kevin Yank的《New-Window Links in a Standards-Compliant World》 《Standards-based Replacement for target="_blank" in External Links》 我们要在新窗口中打开链接通常的做法是在链接后面加target="_blank",我们采用过渡型的DOCTYPE(xhtml1-transitional. dtd)时没有问题,但是当我们使用严格的DOCTYPE(xhtml1-strict.dtd)时,这个方法将通不过W3C的校验,会出现如下错误提示:
"there is no attribute target for this element(in this HTML version)"
function externalLinks() { if (!document.getElementsByTagName) return; var anchors = document.getElementsByTagName("a"); for (var i=0; i var anchor = anchors[i]; if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") anchor.target = "_blank"; } } window.onload = externalLinks; 你可以把它保存成一个.js文件(比如external.js),然后通过外部联接方法调用: