简体   繁体   English

如何使用 javascript/jquery 为播放/暂停按钮制作循环

[英]how to make a loop for a play/pause button using javascript/jquery

so i have been trying to make a music player website and i`m stuck at trying to loop the play/pause script, i kept trying but i reached nothing this is the javascript that i have been using, it does work for the first song but not the others所以我一直在尝试制作一个音乐播放器网站,我一直在尝试循环播放/暂停脚本,我一直在尝试但我什么也没做这是我一直在使用的 javascript,它确实适用于第一首歌曲但不是其他人

<pre>
var mySong = document.getElementById("mySong");
var icon = document.getElementById("icon");
icon.onclick = function () {
  if (mySong.paused) {
    mySong.play();
    icon.src = "/images/pause.png";
  } else {
    mySong.pause();
    icon.src = "/images/play.png";
  }
};
</pre>

and here is the html part that i`m trying to loop这是我试图循环的 html 部分

<pre>
<div class="music-box">
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/a.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
          <hr />
          <div class="music-info">
            <div class="music-img">
              <img src="images/Filosofem.jpg" alt="" />
            </div>
            <div class="music-name">
              <h6>Single Song Title</h6>
              <audio id="mySong">
                <source src="/music/b.mp3" type="audio/mp3" />
              </audio>
              <p>Single name and music director</p>
            </div>
            <img src="/images/play.png" id="icon" style="padding-right: 20px" />
          </div>
        </div>
      </div>
      <div class="music-play">
        <div class="play-image">
          <img src="images/Filosofem.jpg" alt="" />
        </div>
        <div class="play-controls">
          <div class="controls">
            <div class="song-name">
              <h5>Dunkelheit</h5>
              <p>Burzum</p>
            </div>
            <div class="play-icon">
              <i class="fa fa-step-backward"> </i>
              <i class="fa fa-play"> </i>
              <i class="fa fa-step-forward"> </i>
            </div>
          </div>
          <div class="music-progress">
            <div class="progress">
              <div class="progress-bar"></div>
            </div>
          </div>
        </div>
      </div>
    </div>
</pre>

this is my first time using stackoverflow so sorry for my poor documentation,hope you can provide me with solutions or tutorials as this is my first month of learning Front-end.这是我第一次使用stackoverflow,很抱歉我的文档很差,希望你能给我提供解决方案或教程,因为这是我学习前端的第一个月。 ps:- sorry for my poor english as i`m not a native speaker. ps:-对不起我的英语不好,因为我不是母语人士。

I think the problem you have is that you created a EventHandler with jQuery and it looks for some element which have the id of "icon".我认为您遇到的问题是您使用 jQuery 创建了一个 EventHandler,它会查找一些 ID 为“icon”的元素。 But it is not how HTML works.但这不是 HTML 的工作方式。 You have more than one element having same id.您有多个具有相同 ID 的元素。 It's incorrect.这是不正确的。

The correct way of doing this is creating the EventHandler with class of "icon" NOT id of "icon".这样做的正确方法是使用class的“icon”而不是“icon”的 id 创建 EventHandler。 When the handler is triggered, you should change the "src" attribute of this element.触发处理程序时,您应该更改此元素的“src”属性。


After I runned your HTML and JS I realized that you used Object.onclick attribute on your script to trig the function which changes the src attribute. After I runned your HTML and JS I realized that you used Object.onclick attribute on your script to trig the function which changes the src attribute. You could not achieved what you wanted because you are trying to DOM your element with id's.您无法实现您想要的,因为您正在尝试使用 id 对您的元素进行 DOM。 Firstly, convert all id's to class.首先,将所有 id 转换为 class。 And then you should do something like below to set all of the image's onclick attributes to your function.然后您应该执行以下操作,将图像的所有 onclick 属性设置为您的 function。 You can not do it on collection of elements.你不能在元素集合上做到这一点。 You can do it one-by-one with for loop.您可以使用 for 循环一一进行。 ( You better use jQuery for this but since you are new to JS, try this usage and see what happens. ) 您最好使用 jQuery ,但由于您是 JS 新手,请尝试这种用法,看看会发生什么。

var mySong = document.getElementById("mySong");
var icons = document.getElementsByClassName("icon");

var clickFunction = function () {
  alert("kkk");
  if (mySong.paused) {
    mySong.play();
    icon.src = "/images/pause.png";
  } else {
    mySong.pause();
    icon.src = "/images/play.png";
  }
};


for (var i=0; i < icons.length; i++) {
    icons[i].onclick = clickFunction;
};

Hi and welcome to Stack Overflow!您好,欢迎来到 Stack Overflow!

I'm not entirely sure what you mean by "looping the audio", but part of the problem may be that you do not have independent id values for your audio elements in your HTML.我不完全确定“循环播放音频”是什么意思,但部分问题可能是您的 HTML 中的音频元素没有独立的 id 值。 the "id" attribute should give each audio element a unique identifier, but you have given the id of "mySong" to each audio element in your file. “id”属性应该给每个音频元素一个唯一的标识符,但是您已经为文件中的每个音频元素提供了“mySong”的 id。

If you want them to sequentially move from one audio clip to another, you could consider concatenating the audio outside of the browser and then uploading a single audio element.如果您希望它们按顺序从一个音频剪辑移动到另一个,您可以考虑在浏览器外部连接音频,然后上传单个音频元素。 Without more information regarding what specifically you're looking for, it will be difficult to address.如果没有关于您正在寻找的具体内容的更多信息,将很难解决。

First of all, How do you want this code to work when you are using the same id for all audio songs:>, The id is only used once for an only element so to use it u should use class or data-[attribute] and select element, by the way, this is a link about the use of data-[] in short term https://reactgo.com/select-element-data-attribute-js/#:~:text=To%20select%20the%20multiple%20elements,const%20elements%20%3D%20document .首先,当您对所有音频歌曲使用相同的 id 时,您希望此代码如何工作:>,该 id 仅用于唯一元素一次,因此要使用它,您应该使用 class 或 data-[属性]和 select 元素,顺便说一下,这是一个关于短期使用 data-[] 的链接https://reactgo.com/select-element-data-attribute-js/#:~:text=To%20select %20the%20multiple%20elements,const%20elements%20%3D%20document

 const songs = document.querySelectorAll("[data-songs]"); const icons = document.querySelector("#icon"); songs.forEach((song) => { icon.addEventListener('click', function () { //put code of icon here if (mySong.paused) { mySong.play(); icon.src = "/images/pause.png"; } else { mySong.pause(); icon.src = "/images/play.png"; } }) })
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Document</title> </head> <body> <div class="music-box"> <div class="music-info"> <div class="music-img"> <img src="images/Filosofem.jpg" alt="" /> </div> <div class="music-name"> <h6>Single Song Title</h6> <audio id="mySong" data-songs='1'> <source src="test:mp3" type="audio/mp3" /> </audio> <p>Single name and music director</p> </div> <img src="http.//simpleicon.com/wp-content/uploads/play1:png" id="icon" style="padding-right; 20px: height.50px" /> </div> </div> <div class="music-play"> <div class="play-image"> <img src="images/Filosofem.jpg" alt="" /> </div> <div class="play-controls"> <div class="controls"> <div class="song-name"> <h5>Dunkelheit</h5> <p>Burzum</p> </div> <div class="play-icon"> <i class="fa fa-step-backward"> </i> <i class="fa fa-play"> </i> <i class="fa fa-step-forward"> </i> </div> </div> <div class="music-progress"> <div class="progress"> <div class="progress-bar"></div> </div> </div> </div> </div> </body> <script src="app.js"></script> </html>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM