如何使用数组而不是此重复函数中的变量
How do I use an array instead of the variables in this repetitive function?
所以,我的js文件中有这个小代码:
window.onload = function Equal() {
var a = 'b1'
var b = 'box1'
var bookstorname = localStorage.getItem(a)
if (bookstorname == 1) {
document.getElementById(b).setAttribute('checked','checked');
}
if (bookstorname == 0) {
document.getElementById(b).removeAttribute('checked','checked');
}
var a = 'b2'
var b = 'box2'
var bookstorname = localStorage.getItem(a)
if (bookstorname == 1) {
document.getElementById(b).setAttribute('checked','checked');
}
if (bookstorname == 0) {
document.getElementById(b).removeAttribute('checked','checked');
}
}
函数本身并不重要(它等于在本地存储中设置的复选框值(,但我执行了 2 次。var
a
& b
首次设置为 'b1'
& 'box1'
。然后我再次运行脚本(相同的脚本(,但将var
a
和b
设置为'b2'
和'box2'
。现在,这段代码可以工作了,但我的问题是是否有更短的方法来编写它?我可以想象某种带有循环的数组,但由于某种原因我无法让它工作。这两个变量是成对的,我知道这可能是一个愚蠢的问题,但我在任何地方都找不到答案。
您可以使用第二个函数,该函数将接受本地存储密钥和复选框 id,例如
window.onload = function Equal() {
setCheckboxState('box1', 'b1');
setCheckboxState('box2', 'b2');
}
function setCheckboxState(id, key) {
document.getElementById(id).checked = 1 == localStorage.getItem(key);
}
您可以将公共逻辑分离到另一个函数中
window.onload = function Equal() {
function extractFromStorage(a, b) {
var bookstorname = localStorage.getItem(a)
if (bookstorname == 1) {
document.getElementById(b).setAttribute('checked','checked');
}
if (bookstorname == 0) {
document.getElementById(b).removeAttribute('checked','checked');
}
}
extractFromStorage('b1', 'box1');
extractFromStorage('b2', 'box2');
}
function doTheStuff(a, b) {
var bookstorname = localStorage.getItem(a)
if (bookstorname == 1) {
document.getElementById(b).setAttribute('checked','checked');
}
if (bookstorname == 0) {
document.getElementById(b).removeAttribute('checked','checked');
}
}
window.onload = function Equal() {
doTheStuff('b1', 'box1');
doTheStuff('b2', 'box2');
}
?
这就是我会这样做的方式。
您的代码存在几个问题。
- 您不检查要将属性固定到的元素存在。 您不检查您得到的
localStorage
项目是否定义。 - 使用函数名称 Equal 污染全局命名空间。
- 该函数不应以大写字母命名,因为它不是对象生成器。
- 无需使用
setAttribute
和removeAttribute
,在事实上removeAttribute
在这种情况下没有意义,因为你不能从元素中删除选中的属性。顺便说一句,为什么在这里使用setAttribute
而不是window.onload
? checked
属性为真或假,它不使用字符串"已检查">- 通过 onload 属性绑定 load 事件并不安全,因为您可能阻止第三方代码,或者更糟的是第三方代码可能会阻止您。
- 没有错误检查。DOM 页面是动态环境,页面有来自许多地方的广告和内容,可能会干扰您的法典。始终牢记这一点进行编码。检查可能的错误,并以对最终用户友好的方式处理它们。在这种情况下,我使用了警报,对普通用户不友好,但对编码员来说 。
我的解决方案。
// add an event listener rather than replace the event listener
window.addEventListener(
"load", // for the load event
function(){
// the update function that is called for each item;
var update = function(item){
// the right hand side equates to true if the localstorage
// is equal to "1". LocalStorage allways returns a string or
// undefined if the key is not defined.
item.element.checked = localStorage[item.storageName] === "1";
}
// safe element getter
var getElement = function(eId){
var e = document.getElementById(eId); // try and get the element
if(e === null){ // does it exist?
throw "Missing element:"+eId; // no then we can not continue
// the program stops here unless
// you catch the error and deal with
// it gracefully.
}
return e; //ok return the element.
}
// Item creator. This creates a new item.
// sName is the local storage name
// eId id the element ID
var item = function(sName, eId){
return {
storageName: sName, // set the loaclStorage name
element:getElement(eId); // get the element and check its safe
};
}
// make it all safe
try{
// create an array of items.
var items = [
item("b1","box1"),
item("b2","box2")
];
// for each item update the element status
items.forEach(update);
}catch(e){
alert("Could not update page?");
}
}
);
相关文章:
- 访问嵌套函数结构中的JavaScript父函数变量
- Javascript函数变量传递语法问题
- JavaScript 函数变量范围问题
- 在 promise 中获取构造函数变量
- 将函数结果传递给Javascript中的另一个函数变量
- 用对象设置函数变量's值
- Javascript函数变量应生成错误,而不是未定义的错误
- 我可以访问用户脚本中的匿名函数变量吗?
- 在实例化期间使用构造函数变量
- 为什么在 JavaScript 中不能用函数变量覆盖字符串变量
- JavaScript 允许调用函数访问调用函数变量
- 如何在嵌套原型中访问 JavaScript 构造函数变量
- 对函数变量的 JavaScript 控制台日志输出感到困惑
- 如何将函数变量传递给主干模型上的 jQuery 事件
- 如何访问构造函数变量
- 实例函数变量不会更改
- 为什么函数变量在变量之后是未定义的
- 用内部函数变量更改外部函数变量?Javascript
- Javascript - 使用闭包递增静态函数变量模拟
- 异步 JavaScript 静态函数变量