2011-05-16 6 views
0

문자열에 <a href="http://google.com">http://google.com</a>이 포함되어 있다고 가정 해 봅시다. 위의 링크 된 URL과 링크되지 않은 URL이 모두있는 전체 문자열을 연결하면 <a href="<a "" href="http://google.com"">http://google.com"</a>>http://google.com</a>이됩니다.Javascript - 링크 내의 링크를 삭제하는 방법

잘못된 링크 (링크하기 전에 이미 링크 된 링크)를 되돌리려면 <a href="http://google.com">http://google.com</a>으로 되돌릴 수 있습니까?

나는 WordPress에서 이것을 발견하기 위해 $ret = preg_replace("#(]+?>|>))]+?>([^>]+?)#i", "$1$3", $ret); (wp-includes/formatting.php)을 사용합니다. 누군가 나를 JavaScript에서이 작업을 수행하도록 도울 수 있습니까?

+0

에서보세요? –

답변

0

은 왜 linkifying 전에 문자열을 확인 해달라고이 http://jsfiddle.net/mplungjan/V5Qca/

<script> 
function linkify(id,URL) { 
    var container = document.getElementById(id); 
    var links = container.getElementsByTagName("a"); 
    if (links.length ==0) { 
    container.innerHTML=container.innerHTML.link(URL); 
    return; 
    } 
    var nodes = container.childNodes; 
    for (var i=nodes.length-1;i>=0;--i) { 
    if (nodes[i].nodeType ==3 && nodes[i].parentNode.nodeName!="A") { 
     var link = document.createElement("a"); 
     link.href=URL; 
     link.innerHTML=nodes[i].nodeValue; 
     container.replaceChild(link, nodes[i]); 
    } 
    } 
} 
window.onload=function() { 
    linkify("div1","http://www.google.com/"); 
} 
</script> 
<div id="div1"> 
this is a <a href="test">test</a> of replacing text with links with <a href="#">linkified</a> content 
</div> 
관련 문제