[英]Replace a DOM text with an anchor tag
I am searching the html body, but the tag shows in plain text.我正在搜索 html 正文,但标签以纯文本显示。 How do I correct this for an anchor to replace John with the name and a link to show a clickable anchor.
如何更正此锚点以将 John 替换为名称和链接以显示可点击的锚点。 Can the replacement of the search word be solved in javascript?
javascript中搜索词的替换可以解决吗? It is incorrect.
这是不正确的。
I doing search and replace:我做搜索和替换:
htmlreplace(/(John)/gi,"<a href=\"index.html\">$1</a>");
function htmlreplace(a, b, element) {
if (!element) element = document.body;
var nodes = element.childNodes;
for (var n = 0; n < nodes.length; n++) {
if (nodes[n].nodeType == Node.TEXT_NODE) {
var r = new RegExp(a, 'gi');
nodes[n].textContent = nodes[n].textContent.replace(r, b);
} else {
htmlreplace(a, b, nodes[n]);
}
}
}
This is something close to what you are asking.这与您所要求的很接近。 Needs some refactoring, but hope it's a good start for you.
需要一些重构,但希望这对你来说是一个好的开始。
htmlreplace( /(John)/gi, "index.html" );
function htmlreplace( regex, link, element ) {
if ( !element ) { element = document.body; }
let nodes = element.childNodes;
nodes.forEach((node)=>{
if (node.nodeType == Node.TEXT_NODE) {
let nodeTextContent = node.textContent;
let match = regex.exec( nodeTextContent );
if ( match && node.parentNode.tagName !== "SCRIPT" ){
let parentNode = node.parentNode;
let parentHTML = parentNode.innerHTML;
let a = document.createElement('a');
a.href = link;
a.textContent = match[1];
console.log(
match[1],
parentNode.tagName,
parentNode.innerHTML,
);
parentNode.insertBefore( a, node );
parentNode.removeChild( node );
// let addedLink = nodeTextContent.replace( regex, `<a href="${link}">${match[1]}</a>` );
parentNode.innerHTML = parentHTML.replace( regex, `<a href="${link}">${match[1]}</a>` );
// parentNode.innerHTML = addedLink;
// parentNode.innerHTML = nodeTextContent.replace(regex,"") + parentNode.innerHTML;
}
} else {
htmlreplace( regex, link, node );
}
})
}
Here's a Codepen: https://codepen.io/kostasx/pen/VwwLKWz?editors=0010这是一个 Codepen: https://codepen.io/kostasx/pen/VwwLKWz?editors=0010
[ UPDATE ] Code updated to pass the following tests: [更新]更新代码以通过以下测试:
<!-- Test Case #1 [ PASS ] -->
<p>Hello
<span>John</span>
</p>
<!-- Test Case #2 [ PASS ] -->
<h1>Hello John</h1>
<!-- Test Case #3 [ PASS ]-->
<span>
<i>Hello</i> John
</span>
<br>
<!-- Test Case #4 [ PASS ]-->
<span>John hi</span>
<br>
<!-- Test Case #5 [ PASS ]-->
<span>John hi John</span>
I modified the source code of the original javascript.我修改了原javascript的源码。 It works.
有用。 I am thankful it's solved.
我很感激它已经解决了。
htmlreplace( /(John)/gi, "<a href=\"index.php?name=$1\">$1</a>");
function htmlreplace( regex, link, element ) {
if ( !element ) { element = document.body; }
let nodes = element.childNodes;
nodes.forEach((node)=>{
if (node.nodeType == Node.TEXT_NODE) {
let nodeTextContent = node.textContent;
let match = regex.exec( nodeTextContent );
if ( match && node.parentNode.tagName !== "SCRIPT" ){
let parentNode = node.parentNode;
parentNode.removeChild( node );
parentNode.innerHTML = nodeTextContent.replace(regex, link) + parentNode.innerHTML;
}
} else {
htmlreplace( regex, link, node );
}
})
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.