首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >使用jquery搜索单词并突出显示

使用jquery搜索单词并突出显示
EN

Stack Overflow用户
提问于 2015-07-23 15:42:29
回答 2查看 8.7K关注 0票数 4

我已经用jQuery编写了一个提供搜索功能的javaScript文件。我正在试着找出如何也突出这个词。贝罗就是代码。

Filter.js:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
(function ($) {
  // custom css expression for a case-insensitive contains()
  jQuery.expr[":"].Contains = jQuery.expr.createPseudo(function(arg) {
    return function( elem ) {
        return jQuery(elem).text().toUpperCase().indexOf(arg.toUpperCase()) >= 0;
    };
});


function listFilter(header, list, title) { 
   // header is any element, list is an unordered list, title is any element
   // create and add the filter form to the header
   // create a button for collapse/expand to the title

var form = $("<form>").attr({"class":"filterform","action":"#"}),
    button = $("<input>").attr({"class":"rest", "type":"submit", "value":"Collapse All", "id":"switch"}),
    input = $("<input>").attr({"class":"filterinput","type":"text", "placeholder":"Search"});

$(form).append(input).appendTo(header); //add form to header
$(title).append(button); //add button to title

 //on click function for collapse/expand all
$("#switch").click(function(){
    if($(this).val() == "Collapse All"){
        $(".filterinput").val("");
        $(this).val("Expand All");
        $("div.content div.markdown").parent().parentsUntil(list).hide();
        $(list).find("span.path").parentsUntil(list).show();
        $(list).find("ul.endpoints").css("display", "none");
    }
    else{
        $(".filterinput").val("");
        $(this).val("Collapse All");
        $("div.content div.markdown").parent().parentsUntil(list).hide();
        $(list).find("span.path").parentsUntil(list).show();
    }
});

$(input)
  .change( function () {
    var filter = $(this).val();
    if(filter) {
      // this finds a single string literal in div.markdown,
      // and hides the ones not containing the input while showing the ones that do
        $(list).find("div.content div.markdown:not(:Contains(" + filter + "))").parent().parentsUntil(list).hide();
        $(list).find("div.content div.markdown:Contains(" + filter + ")").parent().parentsUntil(list).show();
    } 
    else {
        $("div.content div.markdown").parent().parentsUntil(list).hide();
        $(list).find("span.path").parentsUntil(list).show();
        $(list).find("ul.endpoints").css("display", "none");
    }
    return false;
  })
.keyup( function () {
    // fire the above change event after every letter
    $(this).change();
});
}
   //ondomready
   setTimeout(function () {
    listFilter($("#header"), $("#resources"), $("#api_info"));
   }, 250);
}(jQuery));

我想要操作的html是由另一个JS文件动态创建的,所以我需要在它完全呈现之后操作DOM。我将重点关注的html将呈现为bellow,特别是(div class="markdown")中的单词。

Index.html:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<div class="content" id="connectivitypacks_get_connectivitypacks_content">
    <h4>Description</h4>
    <div class="markdown"><p>Response will return details for the connectivity packs based on the ID.</p>
        <h2 id="keywords">Keywords</h2>
        <p> foo, bar, helloWorld, java</p>
    </div>
</div>
EN

回答 2

Stack Overflow用户

发布于 2015-07-23 18:27:31

这是一个使用你的标记的例子。

用你要搜索的单词创建正则表达式,然后用

  • 获取你的.markdown

  • replace的html,

  • <span class="marker">"+ word +"</span>。因此,这会在搜索的单词周围创建一个跨度标记。

  • 创建css以根据需要设置单词的样式。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
function highlight(word) {
  var element = $('.markdown');
  var rgxp = new RegExp(word, 'g');
  var repl = '<span class="marker">' + word + '</span>';
  element.html(element.html().replace(word, repl));

}

highlight('details');
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
.marker {
  background-color: yellow;
  font-weight: bold;
}
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="content" id="connectivitypacks_get_connectivitypacks_content">
  <h4>Description</h4>
  <div class="markdown">
    <p>Response will return details for the connectivity packs based on the ID.</p>
    <h2 id="keywords">Keywords</h2>
    <p>foo, bar, helloWorld, java</p>
  </div>
</div>

票数 4
EN

Stack Overflow用户

发布于 2016-05-21 14:34:29

看看mark.js吧。它可以在特定上下文中突出显示这样的搜索词。在您的示例中,JavaScript将如下所示:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
var searchTerm = $("#theInput").val();

// Search for the search term in your context
$("div.markdown").mark(searchTerm, {
    "element": "span",
    "className": "highlight"
});

和CSS部分:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
span.highlight{
    background: yellow;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31592132

复制
相关文章

相似问题

添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文