我有这个代码适用于支持jQuery的现代浏览器。但是有时总会有访问者使用非常过时且不支持jQuery的浏览器。
代码目的是用UTF8中文字母搜索和替换英文单词。
任何想法如何调整此代码以使其适用于旧浏览器。
谢谢!
T(#terms) = 2T(#terms/2) + a
T(2) = 2logn + b
答案 0 :(得分:0)
有一些版本的jQuery支持浏览器,直到(包括)IE6。如果您需要支持过时的浏览器,请使用jQuery v1.x行中的最新版本。听起来你正在使用jQuery v2.x甚至是jQuery 3.x,它只支持更新的浏览器。
旁注:您的replaceText
方法没有正确处理特殊的正则表达式字符,并且比它需要的要复杂得多。这是一个更简单的版本,可以正确处理特殊字符:
function replaceText(selector, regex, newText) {
$(selector).not("script, style").each(function() {
var node;
for (node = this.firstChild; node; node = node.nextSibling) {
if (node.nodeType === 3) {
node.nodeValue = node.nodeValue.replace(regex, newText);
}
}
});
}
function replaceAllText() {
// Note that I'm defining the regular expression here, not in `replaceText`
// It's too easy not to realize that you're doing a regex and include a
// character that is special in regular expressions (like the `"."` in
// `"No."` and end up matching things incorrectly.
replaceText('*', /Due/g, '总数');
replaceText('*', /Pin/g, '配套PIN码');
replaceText('*', /No\./g, '编号');
}
function replaceText(selector, regex, newText) {
$(selector).not("script, style").each(function() {
var node;
for (node = this.firstChild; node; node = node.nextSibling) {
if (node.nodeType === 3) {
node.nodeValue = node.nodeValue.replace(regex, newText);
}
}
});
}
function replaceAllText() {
// Note that I'm defining the regular expression here, not in `replaceText`
// It's too easy not to realize that you're doing a regex and include a
// character that is special in regular expressions (like the `"."` in
// `"No."` and end up matching things incorrectly.
replaceText('*', /Due/g, '总数');
replaceText('*', /Pin/g, '配套PIN码');
replaceText('*', /No\./g, '编号');
}
replaceAllText();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
Due
<div>
Stuff
<div>
Pin No. Pin Due
</div>
</div>
</div>
注意我们直接使用DOM来做某些事情;在这种情况下,它就像jQuery代码一样简单。但如果您希望尽可能多地使用jQuery,replaceText
将更改为:
function replaceText(selector, regex, newText) {
$(selector).not("script, style").each(function() {
$(this).contents().each(function() {
if (this.nodeType === 3) {
this.nodeValue = this.nodeValue.replace(regex, newText);
}
});
});
}
function replaceText(selector, regex, newText) {
$(selector).not("script, style").each(function() {
$(this).contents().each(function() {
if (this.nodeType === 3) {
this.nodeValue = this.nodeValue.replace(regex, newText);
}
});
});
}
function replaceAllText() {
// Note that I'm defining the regular expression here, not in `replaceText`
// It's too easy not to realize that you're doing a regex and include a
// character that is special in regular expressions (like the `"."` in
// `"No."` and end up matching things incorrectly.
replaceText('*', /Due/g, '总数');
replaceText('*', /Pin/g, '配套PIN码');
replaceText('*', /No\./g, '编号');
}
replaceAllText();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
Due
<div>
Stuff
<div>
Pin No. Pin Due
</div>
</div>
</div>
最后:我们可以通过使用替换({{1}的正则表达式,使一个通过DOM而不是三个来提高整体效率})然后在旧到新的映射中查找匹配的文本:
this|that|theother
function replaceAllText() {
var replacements = {
"Due": "总数",
"Pin": "配套PIN码",
"No.": "编号"
};
var regex = /(?:Due|Pin|No\.)/g;
$("*:not(script):not(style)").each(function() {
$(this).contents().each(function() {
if (this.nodeType === 3) {
this.nodeValue = this.nodeValue.replace(regex, function(text) {
return replacements[text];
});
}
});
});
}
function replaceAllText() {
var replacements = {
"Due": "总数",
"Pin": "配套PIN码",
"No.": "编号"
};
var regex = /(?:Due|Pin|No\.)/g;
$("*:not(script):not(style)").each(function() {
$(this).contents().each(function() {
if (this.nodeType === 3) {
this.nodeValue = this.nodeValue.replace(regex, function(text) {
return replacements[text];
});
}
});
});
}
replaceAllText();