gpt4 book ai didi

javascript - 在没有框架的情况下如何使 JavaScript 更短/更好?

转载 作者:行者123 更新时间:2023-11-28 13:55:12 24 4
gpt4 key购买 nike

function prepareEventHandlers() {
var sectionButton1 = document.getElementById("sectionButton1");
var sectionButton2 = document.getElementById("sectionButton2");
var sectionButton3 = document.getElementById("sectionButton3");
var sectionButton4 = document.getElementById("sectionButton4");
var sectionButton5 = document.getElementById("sectionButton5");

var enabled1 = true;
var enabled2 = false;
var enabled3 = false;
var enabled4 = false;
var enabled5 = false;


function checkEnabled() {
if (enabled1) {
sectionButton1.setAttribute("class", "sectionButtonEnabled");
}
if (enabled2) {
sectionButton2.setAttribute("class", "sectionButtonEnabled");
}
if (enabled3) {
sectionButton3.setAttribute("class", "sectionButtonEnabled");
}
if (enabled4) {
sectionButton4.setAttribute("class", "sectionButtonEnabled");
}
if (enabled5) {
sectionButton5.setAttribute("class", "sectionButtonEnabled");
}

}

checkEnabled();
sectionButton1.onmouseover = function() {
if (enabled1) {
sectionButton1.setAttribute("class", "sectionButtonOver");
}
};
sectionButton1.onmouseout = function() {
if (enabled1) {
sectionButton1.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton2.onmouseover = function() {
if (enabled2) {
sectionButton2.setAttribute("class", "sectionButtonOver");
}
};
sectionButton2.onmouseout = function() {
if (enabled2) {
sectionButton2.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton3.onmouseover = function() {
if (enabled3) {
sectionButton3.setAttribute("class", "sectionButtonOver");
}
};
sectionButton3.onmouseout = function() {
if (enabled3) {
sectionButton3.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton4.onmouseover = function() {
if (enabled4) {
sectionButton4.setAttribute("class", "sectionButtonOver");
}
};
sectionButton4.onmouseout = function() {
if (enabled4) {
sectionButton4.setAttribute("class", "sectionButtonEnabled");
}
};
sectionButton5.onmouseover = function() {
if (enabled5) {
sectionButton5.setAttribute("class", "sectionButtonOver");
}
};
sectionButton5.onmouseout = function() {
if (enabled5) {
sectionButton5.setAttribute("class", "sectionButtonEnabled");
}
};
}


window.onload = function() {
prepareEventHandlers();
};

最佳答案

任何时候你发现自己写了像“foo1”、“foo2”等这样的变量名,而且它们或多或少都做了相同的事情,你真的需要停止、备份并声明一个数组。

function prepareEventHandlers() {
var sectionButtons = [];
for (var i = 1; i <= 5; ++i)
sectionButtons[i] = document.getElementById('sectionButton' + i);

var enabled = [ true, false, false, false, false ];

function checkEnabled() {
for (var i = 1; i <= 5; ++i)
if (enabled[i]) sectionButtons[i].className = 'sectionButtonEnabled';
}

checkEnabled();

for (i = 1; i <= 5; ++i) {
sectionButton[i].onmouseover = function(i) {
return function() {
if (enabled[i]) sectionButton[i].className = 'sectionButtonOver');
}
}(i);
sectionButton[i].onmouseout = function(i) {
return function() {
if (enabled[i]) sectionButton[i].className = 'sectionButtonEnabled';
}(i);
}
}


window.onload = function() {
prepareEventHandlers();
};

现在,还有两件事:

  1. 不要使用“setAttribute()”设置“class”属性。相反,操作 DOM 元素的“className”属性。
  2. 与其直接将类设置为这些字符串,不如构建自己的“addClass()”和“removeClass()”函数。请记住,类可以是类名的列表,以空格分隔。这些函数看起来像这样:

    function addClass(elem, c) {
    elem.className += ' ' + c;
    }

    function removeClass(elem, c) {
    elem.className = elem.className.replace(new RegExp('\\b' + c + '\\b', 'g'), ''));
    }

关于javascript - 在没有框架的情况下如何使 JavaScript 更短/更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8364165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com