简体   繁体   English

从链接中删除所有 target="_blank"

[英]Remove all target="_blank" from links

I'm messing around with jQuery and ran in to a problem I can't seem to solve.我正在弄乱 jQuery,遇到了一个我似乎无法解决的问题。 I know it's possible with jQuery, but can't find a proper example to work off of.我知道 jQuery 是可能的,但找不到合适的例子来处理。 I have a page with a couple regular links with the attribute/value target="_blank" added to it.我有一个包含几个常规链接的页面,其中添加了属性/值target="_blank"

What's the best approach with jQuery/JavaScript to remove that value from every link on the page?使用 jQuery/JavaScript 从页面上的每个链接中删除该值的最佳方法是什么?

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');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM