单击事件时切换单个函数-Javascript

单击事件时切换单个函数-Javascript,javascript,function,events,click,Javascript,Function,Events,Click,我想调用一个函数,该函数在具有相同类的元素的节点列表上进行切换。我基本上需要在if-else语句中添加函数,但是不同的变体似乎会抛出一个错误。当我将两个函数中的代码直接放入if-else语句中时,它可以工作,但我希望使用函数,因为这是更复杂样式更改的简化版本 代码笔在这里: 代码如下: JS HTML 按钮1 按钮2 您忘记在newColor和originalColor上接收作为参数的事件 var $mainMenuButton = document.getElementsByClassNam

我想调用一个函数,该函数在具有相同类的元素的节点列表上进行切换。我基本上需要在if-else语句中添加函数,但是不同的变体似乎会抛出一个错误。当我将两个函数中的代码直接放入if-else语句中时,它可以工作,但我希望使用函数,因为这是更复杂样式更改的简化版本

代码笔在这里:

代码如下:

JS

HTML

按钮1
按钮2

您忘记在
newColor
originalColor
上接收作为参数的事件

var $mainMenuButton = 
document.getElementsByClassName('desktopmenubutton');

function newColor(e) {
  e.currentTarget.style.background = "black";
}

function originalColor(e) {
  e.currentTarget.style.background = "red";
}

for (h = 0; h < $mainMenuButton.length; h +=1) {
  $mainMenuButton[h].addEventListener('click', function(e) {
    if (e.currentTarget.style.backgroundColor === "red") {
      newColor(e);
    } else {
      originalColor(e);
    }
  });
}
var$main菜单按钮=
document.getElementsByClassName('desktopmenubutton');
函数newColor(e){
e、 currentTarget.style.background=“黑色”;
}
功能原色(e){
e、 currentTarget.style.background=“红色”;
}
对于(h=0;h<$mainMenuButton.length;h+=1){
$mainMenuButton[h]。addEventListener('click',函数(e){
如果(e.currentTarget.style.backgroundColor==“红色”){
新颜色(e);
}否则{
原花色(e);
}
});
}

这应该会起作用。

您忘记在
newColor
originalColor
上接收作为参数的事件

var $mainMenuButton = 
document.getElementsByClassName('desktopmenubutton');

function newColor(e) {
  e.currentTarget.style.background = "black";
}

function originalColor(e) {
  e.currentTarget.style.background = "red";
}

for (h = 0; h < $mainMenuButton.length; h +=1) {
  $mainMenuButton[h].addEventListener('click', function(e) {
    if (e.currentTarget.style.backgroundColor === "red") {
      newColor(e);
    } else {
      originalColor(e);
    }
  });
}
var$main菜单按钮=
document.getElementsByClassName('desktopmenubutton');
函数newColor(e){
e、 currentTarget.style.background=“黑色”;
}
功能原色(e){
e、 currentTarget.style.background=“红色”;
}
对于(h=0;h<$mainMenuButton.length;h+=1){
$mainMenuButton[h]。addEventListener('click',函数(e){
如果(e.currentTarget.style.backgroundColor==“红色”){
新颜色(e);
}否则{
原花色(e);
}
});
}

这应该会起作用。

传递函数中
if
语句中的元素

 var $mainMenuButton = document.getElementsByClassName('desktopmenubutton button1');

    function newColor(element) {
      element.currentTarget.style.backgroundColor = "black";
    }

    function originalColor(element) {
      element.currentTarget.style.backgroundColor = "red";
    }

    for (h = 0; h < $mainMenuButton.length; h +=1) {
      $mainMenuButton[h].addEventListener('click', function(e) {
        if (e.currentTarget.style.backgroundColor === "red") {
          newColor(e);
        } else {
          originalColor(e);
        }
      });
    }
var$mainMenuButton=document.getElementsByClassName('desktopmenubutton button1');
函数newColor(元素){
element.currentTarget.style.backgroundColor=“黑色”;
}
功能原色(元素){
element.currentTarget.style.backgroundColor=“红色”;
}
对于(h=0;h<$mainMenuButton.length;h+=1){
$mainMenuButton[h]。addEventListener('click',函数(e){
如果(e.currentTarget.style.backgroundColor==“红色”){
新颜色(e);
}否则{
原花色(e);
}
});
}

传递函数中
if
语句中的元素

 var $mainMenuButton = document.getElementsByClassName('desktopmenubutton button1');

    function newColor(element) {
      element.currentTarget.style.backgroundColor = "black";
    }

    function originalColor(element) {
      element.currentTarget.style.backgroundColor = "red";
    }

    for (h = 0; h < $mainMenuButton.length; h +=1) {
      $mainMenuButton[h].addEventListener('click', function(e) {
        if (e.currentTarget.style.backgroundColor === "red") {
          newColor(e);
        } else {
          originalColor(e);
        }
      });
    }
var$mainMenuButton=document.getElementsByClassName('desktopmenubutton button1');
函数newColor(元素){
element.currentTarget.style.backgroundColor=“黑色”;
}
功能原色(元素){
element.currentTarget.style.backgroundColor=“红色”;
}
对于(h=0;h<$mainMenuButton.length;h+=1){
$mainMenuButton[h]。addEventListener('click',函数(e){
如果(e.currentTarget.style.backgroundColor==“红色”){
新颜色(e);
}否则{
原花色(e);
}
});
}

您没有传递
e
(事件)参数

还要注意,
Element.style.*
读取内联css样式,而不是在
css
文件/标记中指定的样式

您可以首先在
循环中设置
红色
颜色,以便可以使用
元素.style.*
属性访问它

var$mainMenuButton=document.getElementsByClassName('desktopmenubutton');
函数newColor(e){
e、 currentTarget.style.background=“黑色”;
}
功能原色(e){
e、 currentTarget.style.background=“红色”;
}
对于(h=0;h<$mainMenuButton.length;h+=1){
$mainMenuButton[h].style.background='red';
$mainMenuButton[h]。addEventListener('click',函数(e){
如果(e.currentTarget.style.background==‘红色’){
新颜色(e);
}否则{
原花色(e);
}
});
}
*{
字体系列:arial;
}
.desktopmenubutton{
宽度:150px;
高度:100px;
背景色:红色;
位置:绝对位置;
显示器:flex;
证明内容:中心;
对齐项目:居中;
颜色:白色
}
.按钮2{
左:300px;
}
按钮1

按钮2
您没有传递
e
(事件)参数

还要注意,
Element.style.*
读取内联css样式,而不是在
css
文件/标记中指定的样式

您可以首先在
循环中设置
红色
颜色,以便可以使用
元素.style.*
属性访问它

var$mainMenuButton=document.getElementsByClassName('desktopmenubutton');
函数newColor(e){
e、 currentTarget.style.background=“黑色”;
}
功能原色(e){
e、 currentTarget.style.background=“红色”;
}
对于(h=0;h<$mainMenuButton.length;h+=1){
$mainMenuButton[h].style.background='red';
$mainMenuButton[h]。addEventListener('click',函数(e){
如果(e.currentTarget.style.background==‘红色’){
新颜色(e);
}否则{
原花色(e);
}
});
}
*{
字体系列:arial;
}
.desktopmenubutton{
宽度:150px;
高度:100px;
背景色:红色;
位置:绝对位置;
显示器:flex;
证明内容:中心;
对齐项目:居中;
颜色:白色
}
.按钮2{
左:300px;
}
按钮1
按钮2
 var $mainMenuButton = document.getElementsByClassName('desktopmenubutton button1');

    function newColor(element) {
      element.currentTarget.style.backgroundColor = "black";
    }

    function originalColor(element) {
      element.currentTarget.style.backgroundColor = "red";
    }

    for (h = 0; h < $mainMenuButton.length; h +=1) {
      $mainMenuButton[h].addEventListener('click', function(e) {
        if (e.currentTarget.style.backgroundColor === "red") {
          newColor(e);
        } else {
          originalColor(e);
        }
      });
    }