jQuery highlight.js modification

jQuery highlight.js modification

本文关键字:modification js highlight jQuery      更新时间:2023-09-26

我发现了一个很棒的jQuery高亮插件,但我需要一些修改。我的HTML看起来像:

这是一些文本

这是一些不同的文本

等等。多条线路

我想修改这个插件,就像有人在页面上搜索一个单词一样。隐藏内容div中的所有段落,只显示那些包含突出显示文本的内容。或者,当有人搜索某个类时,会用其他div,比如"隐藏"之类的。感谢的帮助

找到了答案。。。我修改了如下javascript:

$(function() {
jQuery('#text-search').bind('keyup change', function(ev) {
    // pull in the new value
    var searchTerm = jQuery(this).val();
    // remove any old highlighted terms
    jQuery('.toggle').removeHighlight();
    // line to remove the hidden class
    jQuery( ".toggle p" ).removeClass( "hidden" )
    // disable highlighting if empty
    if ( searchTerm ) {
        // highlight the new term
        jQuery('.toggle').highlight( searchTerm );
        //line to add the hidden class
        jQuery('.toggle p:not(:contains('+ searchTerm +'))').closest('p').addClass('hidden');
    }
});