[英]How can I write this javascript code cleaner?
all全部
I am currently practicing my coding skills and am making a simple footer selection webpage.我目前正在练习我的编码技能,并正在制作一个简单的页脚选择网页。
I have four footers with different looks that are set to "display:none" initially.我有四个不同外观的页脚,最初设置为“display:none”。 Then, I have four buttons, each one corresponding to its footer type.然后,我有四个按钮,每个按钮都对应其页脚类型。 When the button is clicked, it displays the footer.单击该按钮时,将显示页脚。
Now I just want to know how do I write a cleaner Javascript than what I currently have.现在我只想知道如何编写比我目前拥有的更干净的 Javascript。 Thank you as always.一如既往地感谢你。
var footer1 = document.getElementById('footer1');
var footer2 = document.getElementById('footer2');
var footer3 = document.getElementById('footer3');
var footer4 = document.getElementById('footer4');
var btn1 = document.getElementById('btn1');
var btn2 = document.getElementById('btn2');
var btn3 = document.getElementById('btn3');
var btn4 = document.getElementById('btn4');
btn1.onclick = function(e) {
console.log('You clicked button1');
e.preventDefault();
footer1.style.display = 'block';
footer2.style.display = 'none';
footer3.style.display = 'none';
footer4.style.display = 'none';
}
btn2.onclick = function(e) {
console.log('You clicked button2');
e.preventDefault();
footer2.style.display = 'block';
footer1.style.display = 'none';
footer3.style.display = 'none';
footer4.style.display = 'none';
}
btn3.onclick = function(e) {
console.log('You clicked button3');
e.preventDefault();
footer3.style.display = 'block';
footer2.style.display = 'none';
footer1.style.display = 'none';
footer4.style.display = 'none';
}
btn4.onclick = function(e) {
console.log('You clicked button4');
e.preventDefault();
footer4.style.display = 'block';
footer2.style.display = 'none';
footer3.style.display = 'none';
footer1.style.display = 'none';
}
You can just use Arrays like this:您可以像这样使用 Arrays :
let buttons = [ 'btn1', 'btn2', 'btn3', 'btn4' ];
let footers = [ 'footer1', 'footer2', 'footer3', 'footer4' ];
buttons.forEach((btn, index) => {
// Please note that you might want to use addEventListener instead of onclick
document.getElementById(btn).addEventListener('click', (event) => {
event.preventDefault();
let button = 'button' + (index + 1);
alert('You clicked ' + button);
footers.forEach((footer, index_f) => {
let f = document.getElementById(footer);
if(index_f === index) {
f.style.display = 'block';
}
else {
f.style.display = 'none';
}
});
});
});
To make things even more interesting, you can play with querySelectorAll
and custom attributes.为了让事情变得更有趣,您可以使用querySelectorAll
和自定义属性。 You could, for example, add the classes custom-button
to your buttons and custom-footer
to your footers, and on each button add a data-footer
attribute pointing to the id of the corresponding footer.例如,您可以将类custom-button
添加到您的按钮和custom-footer
到您的页脚,并在每个按钮上添加一个指向相应页脚 id 的data-footer
属性。 Then, you could do this:然后,您可以这样做:
document.querySelectorAll(".custom-button").forEach((button) => {
button.addEventListener('click', (event) => {
document.querySelectorAll(".custom-footer").forEach(footer => footer.style.display = 'none');
let footer = button.getAttribute("data-footer");
document.getElementById(footer).style.display = 'block';
});
});
Quite shorter.相当短。
There is a common way to do this.有一种常见的方法可以做到这一点。
Then use one function, like然后使用一个 function,比如
function handler = function(e) {
var foos = document.getElementsByClass("foo");
// make all foos display="none"
var target = targets[e.target]; //get the footer to show from the target button
target.style.display = "block";
}
You can use attribute selector to loop over elements.您可以使用属性选择器来循环遍历元素。
[attributeName="value"] [属性名=“值”]
^: Means starts with ^: 表示以
$: Ends with $:以
This way, your logic is generic and does not requires you to maintain any list of IDs这样,您的逻辑是通用的,不需要您维护任何 ID 列表
data-<attr name>
) but you can work with id
s for now.更好的主意是使用数据属性( data-<attr name>
),但您现在可以使用id
。addEventListener
.循环所有按钮并使用addEventListener
添加处理程序。 onClick
is a property, so assignment will erase/ override previous value. onClick
是一个属性,因此赋值将擦除/覆盖以前的值。 You can either have inline anonymous function or a named function if you have too many buttons.如果按钮太多,您可以使用内联匿名 function 或命名 function。this.id
and show necessary footer.使用this.id
获取索引并显示必要的页脚。 Its always better to use classes for such actions instead of setting styles.使用类来执行此类操作总是比设置 styles 更好。 var buttons = document.querySelectorAll('[id^="btn"]'); Array.from(buttons, (button) => { button.addEventListener('click', function(event) { const footers = document.querySelectorAll('[id^="footer"]'); Array.from(footers, (footer) => footer.style.display = 'none' ); const index = this.id.match(/\d+/)[0]; document.getElementById(`footer${index}`).style.display = 'block'; }) })
div[id^="footer"] { width: 100px; height: 100px; border: 1px solid gray; margin: 10px; }
<div id="footer1"> Footer 1 </div> <div id="footer2"> Footer 2 </div> <div id="footer3"> Footer 3 </div> <div id="footer4"> Footer 4 </div> <button id="btn1"> Button 1 </button> <button id="btn2"> Button 2 </button> <button id="btn3"> Button 3 </button> <button id="btn4"> Button 4 </button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.