从数组中查找单词并在其上添加工具提示的脚本



我想制作一个脚本来查找单词列表并用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>

您可以使用<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>

最新更新