在浏览器中使用JavaScript删除链接 [英] Remove links with JavaScript in browser

查看:69
本文介绍了在浏览器中使用JavaScript删除链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  function removehyperlinks(){
try {
alert(document.anchors 。长度);
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');
}

当然,这是测试代码,这就是为什么我有警报和2件事情在同一时间尝试。第一个警报返回0第二个[Object NodeList],第三个返回完成。

我的html正文是这样的:

 < body onload =removehyperlinks()> 
< ol style =text-align:left;类= MessageList中 >
< li class =accesscode>< a href =#>一般资讯,更新,& amp; amp;会议< 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 = >< a href =#> ...< / a>< / li>
< a href =#> ;. ...< / li>
< / ol>
< / li> $ b< / body> $ b< / li> $ b< $ b


解决方案

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





这是有效的当我在控制内容时,我将所有锚点链接使用简单的搜索和重播如果你运行一次,它会取出每一个。所以我只是重复了一遍,正如你所看到的,直到它们全部出来。


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

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".

My html body looks like this:

<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>

解决方案

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.

这篇关于在浏览器中使用JavaScript删除链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆