如何循环Jquery变量选项列表
以下是原始函数*-------------------------------------如何循环Jquery变量选项列表,jquery,Jquery,以下是原始函数*------------------------------------- function lang(t) { $(".OutLbl").each(function () { $(this).html( $(this).html() .replace("Search", diction.wordlist.Search[t]) .replace
function lang(t) {
$(".OutLbl").each(function () {
$(this).html(
$(this).html()
.replace("Search", diction.wordlist.Search[t])
.replace("By", diction.wordlist.By[t])
.replace("Select Language", diction.wordlist["Select Language"][t])
.replace("ID", diction.wordlist["ID"][t])
);
});
};
var diction = {
wordlist: {
"Search": {
dh: "ހޯދާ",
en: "Search"
},
"By": {
dh: "އިން",
en: "by"
},
"Select Language": {
dh: "ބަސް ޙިޔާރުކުރޭ",
en: "Select Language"
},
"ID": {
dh: "އައިޑީ",
en: "ID"
},
}
}
**我需要简化上述函数中的替换,而不是通过
为diction[wordlist]选项中的每个选项循环
有点像**
function lang(t) {
$(diction[wordlist] options).each( function () {
var lll = this;
$(".OutLbl").each(function () {
$(this).html(
$(this).html().replace(lll, diction.wordlist[lll][t])
);
});
});
};
问题是如何在一个函数中循环变量的所有选项(在我的例子中是diction.wordlist),因为我上面尝试的那个选项不起作用 提前谢谢我建议:
var diction = {
wordlist: {
"Search": {
dh: "ހޯދާ",
en: "Search"
},
"By": {
dh: "އިން",
en: "by"
},
"Select Language": {
dh: "ބަސް ޙިޔާރުކުރޭ",
en: "Select Language"
},
"ID": {
dh: "އައިޑީ",
en: "ID"
}
}
};
function lang(t) {
// initialise an array to store all the words/phrases:
var words = [];
// iterate over the properties of the 'diction.wordlist' object:
for (var word in diction.wordlist) {
// if the property is a property of the defined object:
if (diction.wordlist.hasOwnProperty(word)) {
// we push that word into the array:
words.push(word);
}
}
// creating a new RegExp, using the constructor, which allows us
// to use variables. We create the literal regular expression:
// /(Search)|(By)|(Select Language)|(ID)/
// and use case-insensitivity (i) and global (g) flags:
var reg = new RegExp('(' + words.join(')|(') + ')', 'gi');
// using the html method and its anonymous function, which exposes:
// i: the index of the current element amongst the collection, and
// oldhtml: the current innerHTML of the current element (prior to manipulation)
$('.OutLbl ').html(function (index, oldhtml) {
// we return the html after replacing the matchedWords (using the callback
// of replace():
return oldhtml.replace(reg, function (matchedWord) {
// finding the, for example, 'diction.wordlist['Search'][t]:
return diction.wordlist[matchedWord][t];
});
});
}
$('#trigger').on('click', function(e){
e.preventDefault();
lang('dh');
});
参考资料:
- JavaScript:
,使用hasOwnProperty
进行消毒,以避免通过原型继承属性:
$(".OutLbl").each(function () {
var html = $(this).html();
for(var word in diction.wordlist){
if(diction.wordlist.hasOwnProperty(word)) {
html = html.replace(word, diction.wordlist[word][t])
}
}
$(this).html(html);
});
请注意,与当前代码一样,它将仅替换每个单词的第一次出现
要替换所有表达式,请使用带有g
标志的正则表达式,并进行转义
你能澄清你的问题吗?问题是什么?问题是如何在一个函数中循环变量的所有选项(在我的例子中是diction.wordlist),因为我在上面尝试的一个不起作用。相关/可能的dup:谢谢+1这很完美。很高兴能提供帮助。
var regExp = new RegExp(word.replace(/[.^$*+?()[{\|]/g, '\\$&'), "g");
html = html.replace(regExp, diction.wordlist[word][t])