Test if links are external with JavaScript 发表于 2014-12-23 | | 阅读次数 12345678910var internalLinkPattern = new RegExp(location.host);var links = document.querySelectorAll('a');for (var i = 0; i < links.length; i++) { var link = links[i]; internalLinkPattern.test(link.href) ? link.setAttribute('data-target', 'internal') : link.setAttribute('data-target', 'external');} 本文作者: IIssNan 本文链接: http://notes.iissnan.com/2014/test-if-links-are-external-with-javascript/ 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 3.0 CN 许可协议。转载请注明出处!