jquery highlight text 不生效有关问题
jquery highlight text 不生效问题
我用$.getJson()取回的数据组成table的tr和td,但是组合之后要Highlight某些字符串不生效,但是直接把table和js放到jsfiddle上就可以Highlight,是组合table的问题吗?请教应该怎么改?
组成table的代码:
highlight的代码:
------解决思路----------------------
$.getJSON(url, function(result){
//alert(result.response.docs.length);
//alert(result.response.docs[1].key);
project_r = result.response.docs.length;
for (var i = 0; i < result.response.docs.length; i++) {
thisResult = "<tr><td style='font-size:18px;border-bottom:3px #cccccc solid;' class='td_content'>";
$.each( result.response.docs[i], function( x, item ) {
//alert("i:"+i+" item:"+item);
if(x!='_version_' && x!='rownum'){
thisResult = thisResult + x + ":" + item + "<br>";
}
});
thisResult = thisResult + "</td></tr>";
$("#result_table").append(thisResult);
$('.td_content').highlight(search_s);
}
});
$('.td_content').highlight(search_s);
加红色那句,重新高亮一次,要不动态加载的由于异步问题晚于蓝色那句的执行,导致ajax加载的内容没有高亮
我用$.getJson()取回的数据组成table的tr和td,但是组合之后要Highlight某些字符串不生效,但是直接把table和js放到jsfiddle上就可以Highlight,是组合table的问题吗?请教应该怎么改?
组成table的代码:
$.getJSON(url, function(result){
//alert(result.response.docs.length);
//alert(result.response.docs[1].key);
project_r = result.response.docs.length;
for (var i = 0; i < result.response.docs.length; i++) {
thisResult = "<tr><td style='font-size:18px;border-bottom:3px #cccccc solid;' class='td_content'>";
$.each( result.response.docs[i], function( x, item ) {
//alert("i:"+i+" item:"+item);
if(x!='_version_' && x!='rownum'){
thisResult = thisResult + x + ":" + item + "<br>";
}
});
thisResult = thisResult + "</td></tr>";
$("#result_table").append(thisResult);
}
});
$('.td_content').highlight(search_s);
highlight的代码:
$.fn.highlight = function(word) {
var pattern = new RegExp(word, 'g'),
repl = '<span style="background-color:Yellow;">' + word + '</span>';
this.each(function() {
$(this).contents().each(function() {
if(this.nodeType === 3 && pattern.test(this.nodeValue)) {
$(this).replaceWith(this.nodeValue.replace(pattern, repl));
}
else if(!$(this).hasClass('ref')) {
$(this).highlight(word);
}
});
});
return this;
};
------解决思路----------------------
$.getJSON(url, function(result){
//alert(result.response.docs.length);
//alert(result.response.docs[1].key);
project_r = result.response.docs.length;
for (var i = 0; i < result.response.docs.length; i++) {
thisResult = "<tr><td style='font-size:18px;border-bottom:3px #cccccc solid;' class='td_content'>";
$.each( result.response.docs[i], function( x, item ) {
//alert("i:"+i+" item:"+item);
if(x!='_version_' && x!='rownum'){
thisResult = thisResult + x + ":" + item + "<br>";
}
});
thisResult = thisResult + "</td></tr>";
$("#result_table").append(thisResult);
$('.td_content').highlight(search_s);
}
});
$('.td_content').highlight(search_s);
加红色那句,重新高亮一次,要不动态加载的由于异步问题晚于蓝色那句的执行,导致ajax加载的内容没有高亮