【问题标题】:Highlight search keywords result突出显示搜索关键字结果
【发布时间】:2014-03-07 11:20:36
【问题描述】:

我在我的表格中使用以下搜索功能。我需要突出显示与表中搜索输入中的关键字匹配的任何结果。我不知道如何添加脚本。请在这方面帮助我,因为我对 Jscripting 非常陌生并且仍在学习。

window.tableSearch = {};

tableSearch.init = function () {
  this.Rows = document.getElementById('data').getElementsByTagName('TR');
  this.RowsLength = tableSearch.Rows.length;
  this.RowsText = [];
  for (var i = 0; i < tableSearch.RowsLength; i++) {
    this.RowsText[i] = (tableSearch.Rows[i].innerText) ? tableSearch.Rows[i].innerText.toUpperCase() : tableSearch.Rows[i].textContent.toUpperCase();
  }
}

tableSearch.runSearch = function () {
  this.Term = document.getElementById('textBoxSearch').value.toUpperCase();
  for (var i = 0, row; row = this.Rows[i], rowText = this.RowsText[i]; i++) {
    row.style.display = ((rowText.indexOf(this.Term) != -1) || this.Term === '') ? '' : 'none';
  }
}

tableSearch.search = function (e) {
  var keycode;
  if (window.event) {
    keycode = window.event.keyCode;
  } else if (e) {
    keycode = e.which;
  } else {
    return false;
  }
  if (keycode == 13) {
    tableSearch.runSearch();
  } else {
    return false;
  }
}

【问题讨论】:

    标签: javascript search highlight keyword


    【解决方案1】:

    this 线程中的人使用jQuery pluging 突出显示搜索字符串。

    var words = "keyword1,keyword2,keyword3";
    var keywords = words.split(',');
    for(var i = 0; i < keywords.length; i++) {
        $(selector).highlight($.trim(keywords[i]));
    }
    

    你也可以在代码中实现吗?

    【讨论】:

      猜你喜欢
      • 2010-09-16
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 2010-12-10
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多