将锚点替换为它的innerHTML

发布于 2024-08-10 12:35:19 字数 1122 浏览 4 评论 0原文

我想用其innerHTML 替换链接,如何完成?

示例:

Bla bla bla <a href="#nogo">No link here</a> bla bla bla

必须成为

Bla bla bla No link here bla bla bla

尝试用其innerHTML替换节点,但我无法获取节点本身中的文本..

我想我必须使用正则表达式,但我不知道如何操作。

编辑:感谢大家的快速回复!我似乎还是没能搞清楚。看来还有更多事情发生。

我也在使用原型js。

我收到一些可能包含链接的文本。如果文本包含链接,则应将其替换为它的innerHTML。 它看起来像这样:

<td id="text">This is some text <a href="/link/Go_%28To%29" title="Go (to)">goto</a>. Some more text.</td>

我尝试使用 hasChildNodes() 来检查文本是否包含链接,但它总是返回 yes,即使文本不包含链接或其他元素。所以我想也许文本也被认为是一个节点并尝试了 childElements[1]。返回未定义。所以也许链接是文本节点的一个节点。没有什么! 我正在抓取这些数据并评估 JSON,所以奇怪的地方可能就来自于此。不过,在响应的其余部分中遍历 DOM 进展顺利。

但是,当我发出警报时,

$('text').childElements()[0]

实际的 href 就会收到警报! (localhost//link/Go_%28To%29)

$('text'.childElements()[0].up() 让我到达实际的 td 父元素。

所以我使用 来

if($('text').childElements()[0])

检查文本是否包含链接。

我想这有点偏离主题,但我实际上无法正确理解 a 元素。有什么提示吗? 也许正则表达式是最好的选择?

I want to replace a link with its innerHTML, how is it done?

Example:

Bla bla bla <a href="#nogo">No link here</a> bla bla bla

has to become

Bla bla bla No link here bla bla bla

Tried to replace the node with its innerHTML but I can't get the text in the node itself..

I guess I have to work with regular expressions but I can't figure out how.

EDIT: Thank you all for the quick responses! I still can't seem to get it right. It looks like there's more going on.

I am using prototype js as well.

I got some text with could contain a link. If the text contains a link it should be replaced by it's innerHTML.
It looks something like this:

<td id="text">This is some text <a href="/link/Go_%28To%29" title="Go (to)">goto</a>. Some more text.</td>

I tried using hasChildNodes() to check wether the text contained a link but it always returns yes, even when the text did not contain a link or other element. So I thought that maybe the text is considered a node too and tried childElements[1]. That returns undefined. So maybe the link is a node of the textnode then. Nothing!
I am scraping this data and evalling the JSON so the weirdness may come from that. Walking the DOM in the rest of the response goes well though..

However when I alert

$('text').childElements()[0]

The actual href get's alerted! (localhost//link/Go_%28To%29)

$('text'.childElements()[0].up() gets me to the actual td parent element.

So I am using

if($('text').childElements()[0])

To check wether the text contains a link.

I guess this gets a bit off-topic but I actually can't get the a element right. Any tips?
Maybe a regexp is the best option?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

つ低調成傷 2024-08-17 12:35:19

试试这个:

var aElems = document.getElementsByTagName("a");
for (var i=0, n=aElems.length; i<n; ++i) {
    var elem = aElems[i];
    elem.parentNode.replaceChild(document.createTextNode(elem.innerHTML), elem);
}

Try this:

var aElems = document.getElementsByTagName("a");
for (var i=0, n=aElems.length; i<n; ++i) {
    var elem = aElems[i];
    elem.parentNode.replaceChild(document.createTextNode(elem.innerHTML), elem);
}
青丝拂面 2024-08-17 12:35:19

outerHTML 设置为 innerHTML...

window.onload = function() {
  var ancs = document.getElementsByTagName("A");

  for(var i = 0; l = ancs.length; i < l; i++) {
    var anc = ancs[i];
    if(anc.href.substring(0, 1) == "#")
      anc.outerHTML = anc.innerHTML;
  }
}

Set outerHTML to the innerHTML...

window.onload = function() {
  var ancs = document.getElementsByTagName("A");

  for(var i = 0; l = ancs.length; i < l; i++) {
    var anc = ancs[i];
    if(anc.href.substring(0, 1) == "#")
      anc.outerHTML = anc.innerHTML;
  }
}
舟遥客 2024-08-17 12:35:19

我强烈建议使用其中一个 Javascript 库来完成此类工作。

这里有一个使用 Prototype 的单行代码可以解决这个问题:

$('a').each( function( a ) { a.replace( a.innerHTML ) } )

它只是迭代每个 。页面中的标签并将其替换为它的innerHTML。

jQuery 对于此类事情也非常有用。

I highly recommend using one of the Javascript libraries for this sort of work.

Here's a one-liner using Prototype that will do the trick:

$('a').each( function( a ) { a.replace( a.innerHTML ) } )

It simply iterates through each <a> tag in the page and replaces it with its innerHTML.

jQuery is also terrific for this sort of thing.

若言繁花未落 2024-08-17 12:35:19

或者另一种方式,中和链接(没有jquery,我不喜欢它):

创建测试链接:

javascript:var a=document.createElement("a");a.href="#nogo";a.innerText="NOGO";document.body.appendChild(a);void(0);

并取消设置href属性:

javascript:for(var i=0;i<document.links.length;i++)if(document.links[i].hash.toLowerCase()=="#nogo")document.links[i].removeAttribute("href");void(0);

注意,链接仍然有它的样式,你也必须重置它

Or another way, neutralizes a link (w/o jquery, i dislike it):

create test link:

javascript:var a=document.createElement("a");a.href="#nogo";a.innerText="NOGO";document.body.appendChild(a);void(0);

and unset href attribute:

javascript:for(var i=0;i<document.links.length;i++)if(document.links[i].hash.toLowerCase()=="#nogo")document.links[i].removeAttribute("href");void(0);

note, link still have its style, you have to reset it also

傲影 2024-08-17 12:35:19

最后我用一个丑陋的 replace(/<\/?[^>]+(>|$)/g, "\n") 解决了这个问题。还有很多其他事情发生,我没有很好地记录下来,这是我的错。

In the end I solved the problem with an ugly replace(/<\/?[^>]+(>|$)/g, "\n"). There were a lot of other things going on which I haven't documented well, my fault.

北恋 2024-08-17 12:35:19

简而言之,这将完成您的工作:

var a = document.links;

for( var x = 0; x < a.length; x++ ){

a[x].outerHTML = a[x].innerHTML; x-- };

或者,可以使用一些“纯脚本魔法”,或者面向未来的经典,来代替之前的悖论:

var a = document.links;
while( a.length > 0 ){ a[0].outerHTML = a[0].innerHTML }

This would, in short, do your job:

var a = document.links;

for( var x = 0; x < a.length; x++ ){

a[x].outerHTML = a[x].innerHTML; x-- };

Or instead of the previous paradox, one could use some "pure scripting magic", or a future proof classic:

var a = document.links;
while( a.length > 0 ){ a[0].outerHTML = a[0].innerHTML }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文