在浏览器中使用 JavaScript 删除链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2556051/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:54:10  来源:igfitidea点击:

Remove links with JavaScript in browser

javascripthtml

提问by Arlen Beiler

How do I remove links from a webpage with JavaScript? I am using Google Chrome. The code I tried is:

如何使用 JavaScript 从网页中删除链接?我正在使用谷歌浏览器。我试过的代码是:

function removehyperlinks() {
    try {
        alert(document.anchors.length);
        alert(document.getElementsByTagName('a'));
        for(i=0;i=document.anchors.length;i++) {
            var a = document.anchors[i];
            a.outerHTML = a.innerHTML;
            var b = document.getElementsByTagName('a');
            b[i].outerHTML = b[i].innerHTML;
        }
    } catch(e) { alert (e);}
    alert('done');
}

Of course, this is test code, which is why I have the alerts and 2 things trying at the same time. The first alert returns "0" the second [Object NodeList] and the third returns "done".

当然,这是测试代码,这就是为什么我同时尝试警报和两件事的原因。第一个警报返回“0”,第二个 [Object NodeList] 和第三个返回“done”。

My html body looks like this:

我的 html 正文如下所示:

<body onload="removehyperlinks()">
<ol style="text-align:left;" class="messagelist">
    <li class="accesscode"><a href="#">General information, Updates, &amp;   Meetings<span class="extnumber">141133#</span></a>
        <ol>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li><a href="#">...</a></li>
            <li start="77"><a href="#"">...</a></li>
            <li start="88"><a href="#">...</a></li>
            <li start="99"><a href="#">...</a></li>
        </ol>
    </li>
  </ol>
</body>

采纳答案by Arlen Beiler

function removehyperlinks() {
    try {
        for(i=0;i<document.anchors.length;i++) {
            document.anchors[i].outerHTML = document.anchors[i].innerHTML
        }
    } catch(e) { alert ("try2:" + e);}
}
function runner() {
    for(i=1;document.anchors.length > 0;i++) {
        //alert('run ' + i + ':' + document.anchors.length);
        removehyperlinks();
    }
}

This works. As I am in control of the content, I named all the anchors "link" using a simple search and replace. If you run it once, it takes out every other one. So I just had it repeat, as you can see, till they are all out.

这有效。由于我可以控制内容,因此我使用简单的搜索和替换将所有锚点命名为“链接”。如果你运行它一次,它会取出所有其他的。所以我只是重复它,正如你所看到的,直到它们全部用完。

回答by AdmSteck

If you can include jquery, you can do it simply with

如果你可以包含 jquery,你可以简单地用

$('document').ready(function (){
    $('a').contents().unwrap();
});?????????????????

回答by Justin Johnson

Here's some vanilla JS that does the trick. All it does is replace atags with span's and copies over classand idattributes (if they exist).

这是一些可以解决问题的香草 JS。它所做的只是aspan's替换标签并复制classid属性(如果它们存在)。

var anchors = document.getElementsByTagName("A");

for ( var i=0; i < anchors.length; i++ ) {
    var span = document.createElement("SPAN");
    if ( anchors[i].className ) {
        span.className = anchors[i].className;
    }

    if ( anchors[i].id ) {
        span.id = anchors[i].id;
    }

    span.innerHTML = anchors[i].innerHTML;

    anchors[i].parentNode.replaceChild(span, anchors[i]);
}

回答by Deke

You can use removeAttribute:

您可以使用removeAttribute

var allImages = document.querySelectorAll('.imageLinks');

function removehyperlinks()(){  
    for (var i = 0; i < allImages.length; i++) {
    allImages[i].removeAttribute("href");
  }
}

removehyperlinks()()

回答by Robusto

Try

尝试

var ary = document.getElementsByTagName("a");

to get the anchors.

获得锚点。

Then you can remove them like this

然后你可以像这样删除它们

for (var i=0;i<ary.length;i++) {
  // brain cramp: document.removeElement(ary[i]);
  ary[i].parentNode.removeChild(ary[i]);
}