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>ます。

間違ったリンク (リンクする前に既にリンクされているもの) を元に戻す方法はあります<a href="http://google.com">http://google.com</a>か?

$ret = preg_replace("#(]+?>|>))]+?>([^>]+?)#i", "$1$3", $ret);これを達成するために(wp-includes/formatting.phpで)使用することをWordPressで見つけました。JavaScriptでこれを行うのを手伝ってくれる人はいますか?

4

1 に答える 1

0

これを見てください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>  
于 2011-05-16T07:48:13.927 に答える