使用按钮在歌曲之间切换
swapping between songs with a button
我正在尝试通过单击按钮在 2 首歌曲之间切换。我可以通过更改 src 成功地交换一次歌曲,但是当我使用和 if else 语句控件时 "flicker",这意味着我的 if else 语句中一定有一个错误。
HTML
<div id = "audioplayer">
<p id="songname">Listen to Sisyphus by Have A Nice Life</p>
<audio id = "song"
controls
src="Untitled/10%20Sisyphus.mp3">
Your browser does not support the
<code>audio</code> element.
</audio>
</div>
<button onclick="myFunction()">Click to change songs</button>
JS:
function myFunction() {
if(document.getElementById("song").src === "Untitled/10%20Sisyphus.mp3") {
document.getElementById("songname").innerHTML = "listening to track03.mp3";
document.getElementById("song").src = "test.mp3";
}
else{
document.getElementById("songname").innerHTML = "Listen to Sisyphus by Have A Nice Life";
document.getElementById("song").src = "Untitled/10%20Sisyphus.mp3";
}
}
虽然您可以使用相对路径设置 .src
(例如 "Untitled/10%20Sisyphus.mp"
),但访问 .src
属性 会给您 完整 路径:
const song = document.getElementById("song");
song.src = "test.mp3";
console.log(song.src);
<audio id="song" controls src="Untitled/10%20Sisyphus.mp3"></audio>
因此,您只需将测试替换为完整路径,例如:
function myFunction() {
if (document.getElementById("song").src === "https://example.com/Untitled/10%20Sisyphus.mp3") {
// ...
现场片段(工作 HTML,但没有实际声音):
function myFunction() {
const song = document.getElementById("song");
const songname = document.getElementById("songname");
if (song.src === "https://stacksnippets.net/Untitled/10%20Sisyphus.mp3") {
songname.innerHTML = "listening to track03.mp3";
song.src = "test.mp3";
} else {
songname.innerHTML = "Listen to Sisyphus by Have A Nice Life";
song.src = "Untitled/10%20Sisyphus.mp3";
}
}
<div id="audioplayer">
<p id="songname">Listen to Sisyphus by Have A Nice Life</p>
<audio id="song" controls src="Untitled/10%20Sisyphus.mp3">
Your browser does not support the
<code>audio</code> element.
</audio>
</div>
<button onclick="myFunction()">Click to change songs</button>
我正在尝试通过单击按钮在 2 首歌曲之间切换。我可以通过更改 src 成功地交换一次歌曲,但是当我使用和 if else 语句控件时 "flicker",这意味着我的 if else 语句中一定有一个错误。
HTML
<div id = "audioplayer">
<p id="songname">Listen to Sisyphus by Have A Nice Life</p>
<audio id = "song"
controls
src="Untitled/10%20Sisyphus.mp3">
Your browser does not support the
<code>audio</code> element.
</audio>
</div>
<button onclick="myFunction()">Click to change songs</button>
JS:
function myFunction() {
if(document.getElementById("song").src === "Untitled/10%20Sisyphus.mp3") {
document.getElementById("songname").innerHTML = "listening to track03.mp3";
document.getElementById("song").src = "test.mp3";
}
else{
document.getElementById("songname").innerHTML = "Listen to Sisyphus by Have A Nice Life";
document.getElementById("song").src = "Untitled/10%20Sisyphus.mp3";
}
}
虽然您可以使用相对路径设置 .src
(例如 "Untitled/10%20Sisyphus.mp"
),但访问 .src
属性 会给您 完整 路径:
const song = document.getElementById("song");
song.src = "test.mp3";
console.log(song.src);
<audio id="song" controls src="Untitled/10%20Sisyphus.mp3"></audio>
因此,您只需将测试替换为完整路径,例如:
function myFunction() {
if (document.getElementById("song").src === "https://example.com/Untitled/10%20Sisyphus.mp3") {
// ...
现场片段(工作 HTML,但没有实际声音):
function myFunction() {
const song = document.getElementById("song");
const songname = document.getElementById("songname");
if (song.src === "https://stacksnippets.net/Untitled/10%20Sisyphus.mp3") {
songname.innerHTML = "listening to track03.mp3";
song.src = "test.mp3";
} else {
songname.innerHTML = "Listen to Sisyphus by Have A Nice Life";
song.src = "Untitled/10%20Sisyphus.mp3";
}
}
<div id="audioplayer">
<p id="songname">Listen to Sisyphus by Have A Nice Life</p>
<audio id="song" controls src="Untitled/10%20Sisyphus.mp3">
Your browser does not support the
<code>audio</code> element.
</audio>
</div>
<button onclick="myFunction()">Click to change songs</button>