2009-11-05 17 views
0

非固定元素我有一个链接:如何更改链接到文本范围或某种与原型

<a id="theLink" href="h***://stackoverflow.com">Go to SO</a> 

如何使用原型要么剥离<a>的或刚刚离开innerHTML使元素变成:

[h***://stackoverflow.com](不再可点击的东西)?

或许转换<a><span>

回答

2

试试这个

function removeAnchor() { 
     var link = document.getElementById('theLink'); 
     var span = document.createElement("span"); 
     var txt = link.href; 
     var textNode= document.createTextNode(txt); 
     span.appendChild(textNode); 
     link.parentNode.replaceChild(span, link); 
} 
0

要使用<span>与初始元素的href替换<a>的内容:

$('theLink').replace((new Element('span')).update($('theLink').href)); 

结果将成为:

<span>h***://stackoverflow.com</span> 

要使用具有相同内容的替换:

$('theLink').replace((new Element('span')).update($('theLink').innerHTML)); 

将导致其中:

<span>Go to SO</span> 
+0

我认为他想要的东西,纯JavaScript(不jQuery的) – TeKapa 2009-11-17 14:32:47

+1

他说:“我怎么能使用原型...“。他的意思是Prototype JavaScript框架,这就是我的例子所使用的。 – 2009-11-18 19:50:21