如何向元素添加另一个类

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

How To add another class to an element

问题

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

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

这是带有类的图像标签:

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

我想要这样:

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

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

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

以下是播放按钮的脚本:

  1. let play = document.querySelector("#play");
  2. function playsong() {
  3. track.play();
  4. Playing_song = true;
  5. play.innerHTML = '<i class="fa fa-pause" aria-hidden="true"></i>';
  6. }

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

我尝试过在使用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

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

i want to do

  1. <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

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

here is the script for play button

  1. let play = document.querySelector("#play");
  2. function playsong() {
  3. track.play();
  4. Playing_song = true;
  5. play.innerHTML = '<i class="fa fa-pause" aria-hidden="true"></i>';
  6. }

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

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

  1. element.classList.add('elem')

更多关于classList 属性的信息。

英文:

To add a class to an element, do this:

  1. element.classList.add('elem')

Further reading about the classList property.

答案2

得分: 0

这是第一个元素的代码:

  1. let el = document.querySelector('.kiwi')
  2. el.addEventListener('click', (e) => {
  3. e.target.classList.toggle('elem')
  4. })

这是多个元素的代码:

  1. let el = document.querySelectorAll('.kiwi')
  2. el.forEach((item, index) => {
  3. item.addEventListener('click', (e) => {
  4. el.forEach((toRemove) => {
  5. toRemove.classList.remove('elem')
  6. })
  7. e.target.classList.toggle('elem')
  8. })
  9. })
英文:

its for one element:

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

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

  1. let el = document.querySelector(&#39;.kiwi&#39;)
  2. el.addEventListener(&#39;click&#39;, (e) =&gt; {
  3. e.target.classList.toggle(&#39;elem&#39;)
  4. })

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

  1. .elem{
  2. border:1px solid red;
  3. }

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

  1. &lt;ul&gt;
  2. &lt;li class=&quot;kiwi&quot;&gt;1&lt;/li&gt;
  3. &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 -->

  1. let el = document.querySelectorAll(&#39;.kiwi&#39;)
  2. el.forEach((item, index) =&gt; {item.addEventListener(&#39;click&#39;, (e) =&gt; {
  3. el.forEach((toRemove) =&gt; {
  4. toRemove.classList.remove(&#39;elem&#39;)
  5. })
  6. e.target.classList.toggle(&#39;elem&#39;)
  7. })
  8. })

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

  1. .elem{
  2. border:1px solid red;
  3. }

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

  1. &lt;ul&gt;
  2. &lt;li class=&quot;kiwi&quot;&gt;1&lt;/li&gt;
  3. &lt;li class=&quot;kiwi&quot;&gt;2&lt;/li&gt;
  4. &lt;li class=&quot;kiwi&quot;&gt;3&lt;/li&gt;
  5. &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:

确定