问题描述
- 仿百度搜索框智能提示功能,在搜索关键字变色的功能里,输入数字会有问题
-
仿百度搜索框智能提示功能,在搜索关键字变色的功能里,输入数字会有问题,如果数组中含有(1)数字,当文本框第一次按下1这个数字来搜索时,会出现问题,例如:数组中有2123,在文本框输入1,结果会显示2123[[[1]]];如果数组中有两条或以上含有(1)数字的,则搜索结果第一行会这样显示,之后的都正常显示。输入“,”时也有问题,会出现一个标签字符串,原因是因为都“,”来分割的。
贴上我的搜索关键字变色的代码,大家给我看一下吧。
//搜索结果关键字高亮显示function setHeightKeyWord(id, keyword, color, bold) { if (keyword == "") return; var tempHTML = $("#" + id).html(); var htmlReg = new RegExp("<.*?>", "i"); var arrA = new Array(); for (var i = 0; true; i++) { var m = htmlReg.exec(tempHTML); if (m) { arrA[i] = m; } else { break; } tempHTML = tempHTML.replace(m, "[[[[" + i + "]]]]"); } var replaceText if (bold) replaceText = "<b style='color:" + color + ";'>$1</b>"; else replaceText = "<font style='color:" + color + ";'>$1</font>"; var arrayWord = keyword.split(','); for (var w = 0; w < arrayWord.length; w++) { var r = new RegExp("(" + arrayWord[w].replace(/[(){}.+*?^$|\[]]/g, "\$&") + ")", "ig"); tempHTML = tempHTML.replace(r, replaceText); } for (var i = 0; i < arrA.length; i++) { tempHTML = tempHTML.replace("[[[[" + i + "]]]]", arrA[i]); } $("#" + id).html(tempHTML); }
解决方案
你代码就有问题,输入1,替换的html时for (var i = 0; true; i++) { var m = htmlReg.exec(tempHTML);不就出现了 [[[[1]]]]这个,做关键字替换的时候你[[[[这个里面的1也被替换了
所以你的tempHTML = tempHTML.replace("[[[[" + i + "]]]]", arrA[i]);肯定就不匹配了
你遍历子对象进行替换就好了,干嘛要替换html标签
<ul id="ul">
<li>12313</li>
<li>12313</li>
<li>233</li>
<li>12313</li></ul>
<script>
$(function () {
setHeightKeyWord('ul', '1', 'blue');
});
function setHeightKeyWord(id, keyword, color, bold) {
if (keyword == "") return;
var replaceText
if (bold)
replaceText = "<b style='color:" + color + ";'>$1</b>";
else
replaceText = "<font style='color:" + color + ";'>$1</font>";
var arrayWord = keyword.split(',');
var nodes = $('#' + id + '>li');//注意修改子对象选择器
for (var w = 0; w < arrayWord.length; w++) {
for (var i = 0; i < nodes.length; i++) {
var r = new RegExp("(" + arrayWord[w].replace(/[(){}.+*?^$|\[]]/g, "\$&") + ")", "ig");
nodes[i].innerHTML = nodes[i].innerHTML.replace(r, replaceText);
}
}
}
</script>
解决方案二:
//搜索结果关键字高亮显示
function setHeightKeyWord(id, keyword, color, bold) {
if (keyword == "")
return;
var tempHTML = $("#" + id).html();
var htmlReg = new RegExp("<.*?>", "i");
var arrA = new Array();
for (var i = 0; true; i++) {
var m = htmlReg.exec(tempHTML);
if (m) {
arrA[i] = m;
}
else {
break;
}
tempHTML = tempHTML.replace(m, "[[[[" + i + "]]]]");
}
var replaceText
if (bold)
replaceText = "$1";
else
replaceText = "$1";
var arrayWord = keyword.split(',');
for (var w = 0; w < arrayWord.length; w++) {
var r = new RegExp("(" + arrayWord[w].replace(/[(){}.+*?^$|[]]/g, "$&") + ")", "ig");
tempHTML = tempHTML.replace(r, replaceText);
}
for (var i = 0; i < arrA.length; i++) {
tempHTML = tempHTML.replace("[[[[" + i + "]]]]", arrA[i]);
}
$("#" + id).html(tempHTML);
}
解决方案三:
//键盘按下事件
Array.prototype.filter = function (v) { var r = []; for (k in this) if (this[k].indexOf(v) != -1) r.push(this[k]); return r; }
var arr = ["orage", "苹果", "lisa", "1078256","2123", "like", "jack", "abcdefg", "pig", "芒果", "shirly", "xg", "One", "live", "Two", "Four"];
function selectItem(txtConten, event) {
switch (event.keyCode) {
case 38: //上
var li = $('#ul li'), fli = li.filter('.focus');
li.removeClass('focus');
if (fli.length == 0 || fli.prev().length == 0) li.filter(':last').addClass('focus');
else fli.prev().addClass('focus');
break;
case 40: //下
var li = $('#ul li'), fli = li.filter('.focus');
li.removeClass('focus');
if (fli.length == 0 || fli.next().length == 0) li.filter(':first').addClass('focus');
else fli.next().addClass('focus');
break;
case 13: //回车
var li = $('#ul li.focus');
if (li.length > 0) {
$('#txtConten').val(li.text());
li.parent().hide();
}
break;
default:
if (txtConten.value != '') {
var r = arr.filter(txtConten.value);
$('#ul').html($(r).map(function () { return '<li>' + this + '</li>' }).get().join('')).show()
.find('li').click(function () { clearTimeout(timer); $('#txtConten').val(this.innerHTML) });
} else $('#ul').hide();
}
//文本框的值
var $keyword = $("#txtConten").val();
setHeightKeyWord('ul', $keyword, '#1049e8', true);
}