我正在尝试查找字符串中每个出现的字符串,并将其替换为
<span class="blue_color_text">matched_string</span>
这就是我正在使用的(在其中一个问题中找到它):
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
下面是实际的实现:
function showResult(str)
{
if (str.length == 0)
{
return;
}
if (window.XMLHttpRequest)
{
xmlhttp = new XMLHttpRequest();
}
xmlhttp.onreadystatechange = function() {
var e = document.getElementById("res_ls");
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
xmlhttp.responseText.replaceAll(search_string.value, "<span class=\"green\">"+search_string.value+"</span>");
e.innerHTML = xmlhttp.responseText;
e.removeAttribute("class");
}
}
xmlhttp.open("GET","ajax.php?s="+str, true);
xmlhttp.send();
}
String.prototype.replaceAll = function(search, replacement) {
var target = this;
return target.replace(new RegExp(search, 'g'), replacement);
};
实际上什么都没有改变。知道为什么吗?