最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Replace an anchor with its innerHTML - Stack Overflow

programmeradmin1浏览0评论

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 bee

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

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 bee

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

Share Improve this question edited Nov 6, 2009 at 23:05 richard asked Nov 6, 2009 at 21:39 richardrichard 14.6k8 gold badges38 silver badges41 bronze badges 5
  • Do you have any reference points? Like a wrapping span or div or maybe the ID of the link if it has one? – thismat Commented Nov 6, 2009 at 21:41
  • Do you have any requirement to retain any html that may be inside the link itself? Eg <a href="..."><span>foo</span></a>? – Crescent Fresh Commented Nov 7, 2009 at 3:14
  • No there are no cases where there is html inside the link. – richard Commented Nov 7, 2009 at 8:38
  • possible duplicate of Can you provide some examples of why it is hard to parse XML and HTML with a regex? – Brad Mace Commented Jul 9, 2011 at 20:57
  • possible duplicate of RegEx match open tags except XHTML self-contained tags – Paŭlo Ebermann Commented Sep 15, 2011 at 14:13
Add a ment  | 

6 Answers 6

Reset to default 4

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

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

I highly remend 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.

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

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.

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 }
发布评论

评论列表(0)

  1. 暂无评论