如何向元素添加另一个类

huangapple go评论78阅读模式
英文:

How To add another class to an element

问题

以下是翻译好的内容,不包括代码部分:

我有以下带有类名“kiwi”的图像标签,尽管我还有另一个类使图像旋转。当我单击音乐播放器的播放按钮时,我想切换这个类(称为'elem')。

这是带有类的图像标签:

<img src="./images/kiwi.png" class="kiwi" aria-hidden="true" />

我想要这样:

<img src="./images/kiwi.png" class="kiwi **elem**" aria-hidden="true" /> 

这是应该在点击播放按钮时将'class'类添加到图像标签的播放按钮:

<button onclick="justplay()" id="play"><i class="fa fa-play" aria-hidden="true"></i></button>

以下是播放按钮的脚本:

let play = document.querySelector("#play");

function playsong() {
    track.play();
    Playing_song = true;
    play.innerHTML = '<i class="fa fa-pause" aria-hidden="true"></i>';
}

我希望在按下播放按钮时,圆形图像会旋转

我尝试过在使用classList时找不到解决方法。

英文:

i have the following image tag with a class name "kiwi" though i have another class which makes the image spin . i want to toggle this class (called 'elem') when i click on the play button of my music player

this is the image tage with the class

<img src="./images/kiwi.png" class="kiwi" aria-hidden="true" />

i want to do

<img src="./images/kiwi.png" class="kiwi **elem**" aria-hidden="true" /> 

and this is the play button which should add 'elem' class to the image tag onced clicked

<button onclick="justplay()" id="play"><i class="fa fa-play" aria-hidden="true"></i></button>

here is the script for play button

let play = document.querySelector("#play");

function playsong() {
    track.play();
    Playing_song = true;
    play.innerHTML = '<i class="fa fa-pause" aria-hidden="true"></i>';
}

I want the round image to spin when play button is pressed

I tried googling this but i could not figure it out with classList

答案1

得分: 0

要向元素添加一个类,请执行以下操作:

element.classList.add('elem')

更多关于classList 属性的信息。

英文:

To add a class to an element, do this:

element.classList.add('elem')

Further reading about the classList property.

答案2

得分: 0

这是第一个元素的代码:

let el = document.querySelector('.kiwi')

el.addEventListener('click', (e) => {
    e.target.classList.toggle('elem')
})

这是多个元素的代码:

let el = document.querySelectorAll('.kiwi')

el.forEach((item, index) => {
    item.addEventListener('click', (e) => {
        el.forEach((toRemove) => {
            toRemove.classList.remove('elem')
        })
        e.target.classList.toggle('elem')
    })
})
英文:

its for one element:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let el = document.querySelector(&#39;.kiwi&#39;)

el.addEventListener(&#39;click&#39;, (e) =&gt; {
        e.target.classList.toggle(&#39;elem&#39;)
    })

<!-- language: lang-css -->

.elem{
  border:1px solid red;
}

<!-- language: lang-html -->

  &lt;ul&gt;
      &lt;li class=&quot;kiwi&quot;&gt;1&lt;/li&gt;
  &lt;/ul&gt;

<!-- end snippet -->

if you have more .kiwi you need to querySelectorAll and use forEach to add eventListener like THIS:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

let el = document.querySelectorAll(&#39;.kiwi&#39;)

el.forEach((item, index) =&gt; {item.addEventListener(&#39;click&#39;, (e) =&gt; {
        el.forEach((toRemove) =&gt; {
            toRemove.classList.remove(&#39;elem&#39;)
        })
        e.target.classList.toggle(&#39;elem&#39;)
    })
})

<!-- language: lang-css -->

.elem{
  border:1px solid red;
}

<!-- language: lang-html -->

  &lt;ul&gt;
      &lt;li class=&quot;kiwi&quot;&gt;1&lt;/li&gt;
      &lt;li class=&quot;kiwi&quot;&gt;2&lt;/li&gt;
      &lt;li class=&quot;kiwi&quot;&gt;3&lt;/li&gt;
  &lt;/ul&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月27日 13:40:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577074.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定