Heim > Fragen und Antworten > Hauptteil
P粉6708387352023-08-29 15:33:56
const text = YourString.replace(/\bMySearch\b/sg, '<u>$1</u>');
P粉8335469532023-08-29 13:52:37
试试这个:
//获取元素的内部内容 var text = document.getElementById("text").innerHTML; //将要替换的文本用<u>标签包围起来 var newtext = text.replace(/Lorem ipsum <b>amet, consetetur sadipscing elitr/g, "<u>Lorem ipsum <b>amet, consetetur sadipscing elitr</u>"); //将新的下划线文本放回元素中 document.getElementById("text").innerHTML = newtext;
<p id="text"> Lorem ipsum <b>amet, consetetur sadipscing elitr, sed diam</b> dolor sit nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, <i>sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum</i>. Stet clita kasd gubergren, no sea takimata sanctus </p>