var text = document.querySelector(".text"); var search = document.querySelector("input"); const btn = document.querySelector("button"); btn.onclick = () => { // 补全代码 let str = text.innerText; const keywords = search.value.trim(); if(keywords) { str = str.replace(new RegExp(keywords,'g'), `<b style="background-color: yellow;">${keywords}</b>` ) } text.innerHTML = str; }