mark.js - 使用next / previous / clear函数突出显示URL Referrer

时间:2017-05-12 08:48:17

标签: jquery highlight referrer

我想实施mark.js突出显示,以便突出显示来自我的Google自定义搜索引擎的搜索字词。这可以按照“URL Referrer突出显示”提供的示例完成。我希望能够添加下一个/上一个和清除功能,以帮助用户点击匹配,以及更改搜索条件,如“搜索栏跳转到匹配”的示例中所示。我无法让这个工作......任何人都可以帮忙吗?

这是URL Referrer突出显示的工作小提琴: https://jsfiddle.net/lbriquet/axw5af8k/

我想加入Next |上一页|清除按钮,允许用户通过搜索匹配工作。

上一个/下一个/清除功能有一个小提琴:https://jsfiddle.net/julmot/973gdh8g/

他们使用的代码是:

    $(function() {

      // the input field
      var $input = $("input[type='search']"),
    // clear button
    $clearBtn = $("button[data-search='clear']"),
    // prev button
    $prevBtn = $("button[data-search='prev']"),
    // next button
    $nextBtn = $("button[data-search='next']"),
    // the context where to search
    $content = $(".content"),
    // jQuery object to save <mark> elements
    $results,
    // the class that will be appended to the current
    // focused element
    currentClass = "current",
    // top offset for the jump (the search bar)
    offsetTop = 50,
    // the current index of the focused element
    currentIndex = 0;

    /**
     * Jumps to the element matching the currentIndex
    */
    function jumpTo() {
        if ($results.length) {
          var position,
            $current = $results.eq(currentIndex);
          $results.removeClass(currentClass);
          if ($current.length) {
            $current.addClass(currentClass);
            position = $current.offset().top - offsetTop;
            window.scrollTo(0, position);
          }
        }
      }

    /**
    * Searches for the entered keyword in the
    * specified context on input
    */
    $input.on("input", function() {
    var searchVal = this.value;
    $content.unmark({
    done: function() {
    $content.mark(searchVal, {
              separateWordSearch: true,
              done: function() {
                $results = $content.find("mark");
                currentIndex = 0;
                jumpTo();
              }
            });
          }
      });
    });

    /**
    * Clears the search
    */
    $clearBtn.on("click", function() {
    $content.unmark();
    $input.val("").focus();
      });

      /**
       * Next and previous search jump to
       */
      $nextBtn.add($prevBtn).on("click", function() {
        if ($results.length) {
          currentIndex += $(this).is($prevBtn) ? -1 : 1;
          if (currentIndex < 0) {
            currentIndex = $results.length - 1;
          }
          if (currentIndex > $results.length - 1) {
            currentIndex = 0;
          }
          jumpTo();
        }
      });
    });

0 个答案:

没有答案