简体   繁体   English

我正在尝试优化第一块代码。 为什么我的方法不起作用? 缩短代码的最佳方法是什么?

[英]I am trying to optimize the first chunk of code. Why doesn't my approach work? What's the best approach to shorten the code?

The code I try to optimize:我尝试优化的代码:

function classToggle1() {
    document.getElementById("div1").classList.toggle('fa-folder');
    document.getElementById("div1").classList.toggle('fa-folder-open');
}
function classToggle2() {
    document.getElementById("div2").classList.toggle('fa-folder');
    document.getElementById("div2").classList.toggle('fa-folder-open');
}
function classToggle3() {
    document.getElementById("div3").classList.toggle('fa-folder');
    document.getElementById("div3").classList.toggle('fa-folder-open');
}
document.querySelector('#x').addEventListener('click', classToggle1);
document.querySelector('#y').addEventListener('click', classToggle2);
document.querySelector('#z').addEventListener('click', classToggle3);

My approach that doesn't work in browsers:我的方法在浏览器中不起作用:

function classToggle(x) {
    document.getElementById(x).classList.toggle('fa-folder');
    document.getElementById(x).classList.toggle('fa-folder-open');
}
document.querySelector('#xx').addEventListener('click', classToggle("div1"));
document.querySelector('#yy').addEventListener('click', classToggle("div2"));
document.querySelector('#zz').addEventListener('click', classToggle("div3"));

Here I pass in "div1" "div2" and "div3" as arguments.在这里,我将“div1”“div2”和“div3”作为 arguments 传递。 My understanding is that those two code chunks should behave the same?我的理解是这两个代码块的行为应该相同?

In the first example you're passing in the function ( classToggle1 ), and in the second you're invoking the function (which happens to return undefined ).在第一个示例中,您传入 function ( classToggle1 ),而在第二个示例中,您正在调用 function (恰好返回undefined )。 You can update the second to enlambda the call.您可以更新第二个以 enlambda 调用。

document.querySelector('#xx').addEventListener('click', () => classToggle("div1"));

Or或者

document.querySelector('#xx').addEventListener('click', function () { classToggle("div1"); })

This should work.这应该有效。

document.querySelector('#xx').addEventListener('click', () => classToggle("div1"));
document.querySelector('#yy').addEventListener('click', () => classToggle("div2"));
document.querySelector('#zz').addEventListener('click', () => classToggle("div3"));

The point is that, you are passing the output of the classToggle(x) instead of passing the function itself.关键是,您传递的是 classToggle(x) 的 output 而不是传递 function 本身。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么当我尝试将函数的返回值与字符串连接时我的代码不起作用? - why my code doesn't work when I am trying to concatenate a function's return value with a string? 循环学习。 无法弄清楚我的代码中缺少什么。 - Learning While Loops. Can't figure out what I am missing in my code. 为什么这个 JS 和 PHP 代码不起作用? (我正在尝试将值从 JS 发送到 PHP) - Why doesn't this JS & PHP code work? (I am trying to send a value from JS to PHP) 我正在尝试将Material-UI InfoIcon添加到我的代码中。 但我不知道如何在TextField中实现它 - I am trying to add the Material-UI InfoIcon into my code. But I don't know how to implement it in TextField 使我的 HTML 代码以最少的重写代码量响应移动设备的最佳方法是什么? - What is the best approach to make my HTML code responsive for mobile with minimal amount of re-writing code? 我正在尝试通过php代码更新记录。 但是我的代码有麻烦 - I am trying to update records by php code. but having trouble in my code 我的“t”在控制台中未定义,我不知道我的代码有什么问题。 如何在此代码中定义我的“t” - Am getting my "t" as undefined in the console and i do not know what is wrong with my code. How can I define my 't' in this code 我不明白为什么我的代码不起作用有人可以检查一下吗? 我很新 (JavaScript) - i can't understand why my code doesn't work can someone please check it out? I am very new (JavaScript) 我正在尝试遵循教程并且教程代码有效,但我的代码不起作用 - I am trying to follow a tutorial and the tutorial code works but my code doesn't 我无法使用JavaScript代码访问CSS样式。 我的代码有什么问题? - I can't access a css style with my javascript code. what is wrong with my code?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM