【发布时间】: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