search

Home  >  Q&A  >  body text

javascript - Is there any plug-in related to text highlighting in the input box? Can you recommend it?

To create a keyword search function

迷茫迷茫2741 days ago632

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-19 10:18:27

    If it’s just a very ordinary highlight. . . You can do this:

    .hl {
        background-color: yellow; 
    }
    <p class="search-result">搜索结果: SegmentFault ( www.sf.gg ) 是中国领先的开发者社区。
    我们希望为中文开发者提供一个纯粹、高质的技术交流平台,
    与开发者一起学习、交流与成长,创造属于开发者的时代!</p>
    function light($sel, keyword, hightLightClass){
        var text = $sel.html(); 
        var temp = text.split(keyword).join(`<span class="${hightLightClass}">${keyword}</span>`); 
        $sel.html(temp);
    }
    
    light($('.search-result'), '交流', 'hl'); 

    Example

    reply
    0
  • Cancelreply