我正在使用 HTML / Javascript 开发屏幕键盘。键盘是在运行时生成的。
我正在使用 JSON 数据结构来存储不同的键盘布局、语言设置等。
var initParams = {
keyboardLanguage: "de",
keyboardLayout: {
de: {
row1: ["Q","W","E","R","T","Z","U","I","O","P"],
row2: ["A","S","D","F","G","H","J","K","L"],
row3: ["Y","X","C","V","B","N","M"]
},
en: {
row1: ["Q","W","E","R","T","Y","U","I","O","P"],
row2: ["A","S","D","F","G","H","J","K","L"],
row3: ["Z","X","C","V","B","N","M"]
}
}
}
这是完成这项工作的功能:
function CreateKeyHtmlElements(){
var divElement = document.createElement("div");
for(var obj in initParams.keyboardLayout[initParams.keyboardLanguage]){
var keyRow = divElement.cloneNode(false);
keyRow.id = "key" + obj;
keyRow.className = "keyboard-key-row";
for(var i = 0, l = initParams.keyboardLayout[initParams.keyboardLanguage][obj].length; i < l; i +=1){
var key = divElement.cloneNode(false);
key.id = "key-" + initParams.keyboardLayout[initParams.keyboardLanguage][obj][i];
key.className = "keyboard-key";
key.textContent = initParams.keyboardLayout[initParams.keyboardLanguage][obj][i];
keyRow.appendChild(key);
$(key).appendTo(keyRow);
}
$(keyRow).appendTo($("#keyboard-key-box"));
}
}
是否有任何有效的方法来优化这个功能,特别是循环,使用 jQuery。
提前致谢。