Highlight a word with jQuery

后端 未结 12 1369
粉色の甜心
粉色の甜心 2020-11-22 01:20

I basically need to highlight a particular word in a block of text. For example, pretend I wanted to highlight the word "dolor" in this text:



        
12条回答
  •  孤独总比滥情好
    2020-11-22 01:44

    $(function () {
        $("#txtSearch").keyup(function (event) {
            var txt = $("#txtSearch").val()
            if (txt.length > 3) {
                $("span.hilightable").each(function (i, v) {
                    v.innerHTML = v.innerText.replace(txt, "" + txt + "");
                });
    
            }
        });
    });
    

    Jfiddle here

提交回复
热议问题