我想制作一个查找单词列表并将其替换为href链接的脚本。例如
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace("barbel", "<https://www.youtube.com/watch?v=XiFkyR35v2Y");
document.getElementById("demo").innerHTML = res;
}
<p id="demo">Visit barbel</p>
<button onclick="myFunction()">Try it</button>
答案 0 :(得分:0)
您可以使用<a>
来显示链接:
function myFunction() {
var str = document.getElementById("demo").innerHTML;
var res = str.replace("barbel", "<a href='https://www.youtube.com/watch?v=XiFkyR35v2Y'>here</a>");
document.getElementById("demo").innerHTML = res;
}
<p id="demo">Visit barbel</p>
<button onclick="myFunction()">Try it</button>