我最近才开始学习 HTML 和 HTML5。我的网站涉及不同的音频播放器播放不同的曲调/mp3。目前,我为每个音频播放器和每个播放/暂停和停止按钮创建了不同的 ID 名称。
我播放这个的 JavaScript 文件不必要地重复了类似的代码。我想有一种更好的方法可以使用变量来执行此操作,因此代码不会以不同的 id 名称重复,但我对 JavaScript 不太熟悉。
这是我的 JavaScript 文件片段的样子。
$(document).ready(function() {
$("#play-bt1").click(function() {
$("#audio-player1")[0].play();
$("#message").text("Music started");
})
$("#pause-bt1").click(function() {
$("#audio-player1")[0].pause();
$("#message").text("Music paused");
})
$("#stop-bt1").click(function() {
$("#audio-player1")[0].pause();
$("#audio-player1")[0].currentTime = 0;
$("#message").text("Music Stopped");
})
})
$(document).ready(function() {
$("#play-bt2").click(function() {
$("#audio-player2")[0].play();
$("#message").text("Music started");
})
$("#pause-bt2").click(function() {
$("#audio-player2")[0].pause();
$("#message").text("Music paused");
})
$("#stop-bt2").click(function() {
$("#audio-player2")[0].pause();
$("#audio-player2")[0].currentTime = 0;
$("#message").text("Music Stopped");
})
})
$(document).ready(function() {
$("#play-bt3").click(function() {
$("#audio-player3")[0].play();
$("#message").text("Music started");
})
$("#pause-bt3").click(function() {
$("#audio-player3")[0].pause();
$("#message").text("Music paused");
})
$("#stop-bt3").click(function() {
$("#audio-player3")[0].pause();
$("#audio-player3")[0].currentTime = 0;
$("#message").text("Music Stopped");
})
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section class="grid_12" id="content">
<div class="grid_4 alpha">
<div class="audiobox">
<audio id="audio-player" name="audio-player" src="images/electronic beat-monksee.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt">
<div class="animated_play"></div>
</a>
<a id="pause-bt">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<div class="grid_4">
<div class="audiobox">
<audio id="audio-player1" name="audio-player" src="images/electronic beat-monksee.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt1" href="#">
<div class="animated_play"></div>
</a>
<a id="pause-bt1" href="#">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<div class="grid_4 omega">
<div class="audiobox">
<audio id="audio-player2" name="audio-player" src="images/beatbox.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt2">
<div class="animated_play"></div>
</a>
<a id="pause-bt2">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<div class="grid_4 alpha">
<div class="audiobox">
<audio id="audio-player3" name="audio-player" src="images/electronic beat-monksee.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt3">
<div class="animated_play"></div>
</a>
<a id="pause-bt3">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<div class="grid_4">
<div class="audiobox">
<audio id="audio-player4" name="audio-player" src="images/electronic beat-monksee.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt4">
<div class="animated_play"></div>
</a>
<a id="pause-bt4">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<div class="grid_4 omega">
<div class="audiobox">
<audio id="audio-player5" name="audio-player" src="images/beatbox.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt5">
<div class="animated_play"></div>
</a>
<a id="pause-bt5">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<div class="grid_4 alpha">
<div class="audiobox">
<audio id="audio-player6" name="audio-player" src="images/electronic beat-monksee.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt6">
<div class="animated_play"></div>
</a>
<a id="pause-bt6">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<div class="grid_4">
<div class="audiobox">
<audio id="audio-player7" name="audio-player" src="images/electronic beat-monksee.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt7">
<div class="animated_play"></div>
</a>
<a id="pause-bt7">
<div class="animated_stop"></div>
</a>
</div>
</div>
</div>
<feature class="grid_4 omega">
<div class="audiobox">
<audio id="audio-player8" name="audio-player" src="images/beatbox.mp3">audio</audio>
<div class="controlsbox">
<a id="play-bt8">
<div class="animated_play"></div>
</a>
<a id="pause-bt8">
<div class="animated_stop"></div>
</a>
</div>
</div>
</feature>
</section>