简体   繁体   English

使用JavaScript将超链接转换为纯文本

[英]Convert hyperlinks to plain text using JavaScript

Using JavaScript, is it possible to convert a hyperlink to plain text (so that it no longer links to any page?) 使用JavaScript,是否可以将超链接转换为纯文本(以便不再链接到任何页面?)

For example, would it be possible to change the following link (inside the <a> tag) to plain text using JavaScript (so that this text no longer links to any page)? 例如,是否可以使用JavaScript将以下链接(在<a>标记内)更改为纯文本(以使该文本不再链接至任何页面)?

<html>
    <body>
        <a href="http://www.wikipedia.org/" target="blank" id = "wikiLink">Go to Wikipedia</a>
    </body>
</html>

The simplest way would be to remove its href attribute: 最简单的方法是删除其href属性:

document.getElementById('wikiLink').removeAttribute("href");

If you want to completely remove the node, leaving the text, try this: 如果要完全删除节点,而保留文本,请尝试以下操作:

var toRemove = document.getElementById('wikiLink'), parent = toRemove.parentNode,
    text = toRemove.firstChild;
parent.insertBefore(text,toRemove);
parent.removeChild(toRemove);
parent.normalize();
document.getElementById("wikiLink").removeAttribute('href')

或与jQuery

$("#wikiLink").removeAttr('href');

Following code should render hyper link as text. 以下代码应将超级链接呈现为文本。

$(element).find('a').contents().unwrap(); $(element).find('a')。contents()。unwrap();

JS BIN Demo JS BIN演示

If you want to remove the "link" behavior of all links, you can do: 如果要删除所有链接的“链接”行为,则可以执行以下操作:

var links = document.getElementsByTagName('a');
for(var i=0; i<links.length; i++) links[i].href = '';

With jQuery a simple way is to remove the link and to put a text node instead: 使用jQuery的一种简单方法是删除链接并放置一个文本节点:

var $link = $('#wikiLink');
$link.after($link.text());
$link.remove();

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

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