Javascript 从链接中删除所有 target="_blank"
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10098898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove all target="_blank" from links
提问by Dustin
I'm messing around with jQuery and ran in to a problem I can't seem to solve. I know it's possible with jQuery, but can't find a proper example to work off of. I have a page with a couple regular links with the attribute/value target="_blank"
added to it.
我正在使用 jQuery 并且遇到了一个我似乎无法解决的问题。我知道使用 jQuery 是可能的,但找不到合适的例子来解决。我有一个页面,其中有几个常规链接,其中target="_blank"
添加了属性/值。
What's the best approach with jQuery/JavaScript to remove that value from every link on the page?
使用 jQuery/JavaScript 从页面上的每个链接中删除该值的最佳方法是什么?
回答by alex
This should do it with jQuery...
这应该用jQuery来做...
$('a[target="_blank"]').removeAttr('target');
With a modern browser...
使用现代浏览器...
Array.from(document.querySelectorAll('a[target="_blank"]'))
.forEach(link => link.removeAttribute('target'));
With an older browser such as earlier IEs...
使用较旧的浏览器,例如较早的 IE...
var links = document.links, i, length;
for (i = 0, length = links.length; i < length; i++) {
links[i].target == '_blank' && links[i].removeAttribute('target');
}