【问题标题】:JavaScript Toggle not working with many elements with the same class attributeJavaScript Toggle 不适用于具有相同类属性的许多元素
【发布时间】:2020-11-03 20:25:57
【问题描述】:

我正在尝试为“活动”CSS 类切换“禁用”,以使某些 SVG 的填充属性在单击时发生变化。

我能够正确更改第一个元素,但是在尝试对第二个和第三个 SVG 进行相同操作时,它会更改第一个 div 中第一个元素的颜色。

HTML

   <div>
       <svg onclick="toggleColor()" class="home__like disable heart">
           <use xlink:href="img/sprite.svg#icon-heart-full"></use>
       </svg>
   </div>
   <div>
       <svg onclick="toggleColor()" class="home__like disable heart">
           <use xlink:href="img/sprite.svg#icon-heart-full"></use>
       </svg>
   </div>
   <div>
       <svg onclick="toggleColor()" class="home__like disable heart">
           <use xlink:href="img/sprite.svg#icon-heart-full"></use>
       </svg>
   </div>

CSS

.disable {
    fill: #fff;
}

.active {
    fill: $color-primary;
}

JavaScript

function toggleColor() {

    const toggleHeart = document.querySelector('.heart');

    if(toggleHeart.classList.contains('disable')) {
        toggleHeart.classList.remove('disable');
        toggleHeart.classList.add('active');
    } else {
        toggleHeart.classList.remove('active');
        toggleHeart.classList.add('disable');
    }
}

【问题讨论】:

  • 您是否在文档中添加了脚本标签?如果有,是否添加到页面底部?

标签: javascript html css toggle


【解决方案1】:

您正在使用.querySelector(),它返回文档中与提供的选择器匹配的第一个元素。

要获取多个元素,您需要使用.querySelectorAll(),它将返回与选择器匹配的静态元素NodeList。此时,您需要遍历 NodeList 并操作类。

但是,由于您尝试以事件调用元素为目标,我认为您可以通过引用该元素来简化它。

function toggleColor(el, e) {
    el.classList.toggle('disable');
    el.classList.toggle('active');
}

并将您的 onclick 处理程序更改为 onclick="toggleColor(this,event);"

这是一个快速的 sn-p:

function toggleColor(el, e) {
    el.classList.toggle('disable');
    el.classList.toggle('active');
}
.disable { color: #ccc }
.active  { color: #0095ee; }
<div>
    <svg class="disable" onclick="toggleColor(this,event);" viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" stroke-width="2" fill="currentColor" stroke-linecap="round" stroke-linejoin="round" class="css-i6dzq1"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"></path></svg>
 </div>
 <div>
     <svg class="disable" onclick="toggleColor(this,event);" viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" stroke-width="2" fill="currentColor" stroke-linecap="round" stroke-linejoin="round" class="css-i6dzq1"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"></path></svg>
 </div>
 <div>
     <svg class="disable" onclick="toggleColor(this,event);" viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" stroke-width="2" fill="currentColor" stroke-linecap="round" stroke-linejoin="round" class="css-i6dzq1"><path d="M20.84 4.61a5.5 5.5 0 0 0-7.78 0L12 5.67l-1.06-1.06a5.5 5.5 0 0 0-7.78 7.78l1.06 1.06L12 21.23l7.78-7.78 1.06-1.06a5.5 5.5 0 0 0 0-7.78z"></path></svg>
 </div>

【讨论】:

  • 嘿,伙计,刚刚玩过你的代码,效果很好。我不知道所有相同类的选择器以及带有“this”和“event”的处理程序。感谢您的宝贵时间
【解决方案2】:

为什么会这样?

由于heart 类有多个元素,它不知道要在哪个元素上执行脚本!这就像一位老师试图给 Bob 打电话,而班级中有 5 个名为 Bob 的人。

我们如何解决这个问题?

您可以通过在脚本中提供元素的引用来更改onclick 中的函数,如下所示:onclick="myFunction(this)"。现在在 javascript 中,你可以对元素做任何你需要的事情。

试试吧!运行下面的例子!

function toggleColor(element) {

    toggleHeart = element;

    if(toggleHeart.classList.contains('disable')) {
        toggleHeart.classList.remove('disable');
        toggleHeart.classList.add('active');
    } else {
        toggleHeart.classList.remove('active');
        toggleHeart.classList.add('disable');
    }
}
.disable {
    color: blue;
}

.active {
    color: red;
}
<!--                      vvv  below we used the "this" keyword which references to the svg element -->
<div onclick="toggleColor(this)" class="disable">
    Click Me!
</div>
<div onclick="toggleColor(this)" class="disable">
    Click Me!
</div>
<div onclick="toggleColor(this)" class="disable">
    Click Me!
</div>
<div onclick="toggleColor(this)" class="disable">
    Click Me!
</div>

【讨论】:

  • 也与此一起工作,发现了使简单动作起作用的多种方法。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多