Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
295 views
in Technique[技术] by (71.8m points)

javascript - How to get id of pause button along with play button id when play button is clicked?

I want to get the id of the pause button along with the play button id when the play button is clicked? As I want multiple play button. so I need a function that could serve all buttons. Here I have to first fetch play button id then pause button id. Also i have to write id of pause button like document.getElementById('pausebutton'); I want to fetch its Id in function too. I am not able to get both button ids in one function.

<!DOCTYPE html>
<html lang="en-US">
<head>
<title>Page Title</title>
<meta charset="UTF-8">
  <meta name="description" content="Free Web tutorials">
  <meta name="keywords" content="HTML, CSS, JavaScript">
  <meta name="author" content="John Doe">
  <meta http-equiv="refresh" content="30">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>

<i class="fa fa-play" style="font-size:24px;" id="playbutton" onclick="togglePlay('play',document.getElementById('myAudio'),this.id)"></i>
<i class="fa fa-pause" style="font-size:24px;display:none;" id="pausebutton" onclick="togglePause('pause',document.getElementById('myAudio'),this.id)"></i>

<a href="http://localhost/bkportfolio/wp-content/uploads/2021/01/ENG_US_M_DaveL.mp3" download="ENG_US_M_DaveL.mp3"><button>Download</button></a>

<audio id="myAudio">
  <source src="http://localhost/bkportfolio/wp-content/uploads/2021/01/ENG_US_M_DaveL.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
<script>
function togglePlay(state,aid,playid) {
var p=playid;

    var audio = aid,
        play  = document.getElementById(p),
        pause = document.getElementById('pausebutton');

    if (state == 'play') {
        audio.play();
        play.style.display = 'none';
        pause.style.display = 'block';
    }
}
function togglePause(state,aid,pauseid) {
var p=pauseid;

    var audio = aid,
        pause  = document.getElementById(p),
        play = document.getElementById('playbutton');

    if (state == 'pause') {
        audio.pause();
        play.style.display = 'block';
        pause.style.display = 'none';
    }
}

</script>
</body>
</html>
question from:https://stackoverflow.com/questions/65868705/how-to-get-id-of-pause-button-along-with-play-button-id-when-play-button-is-clic

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Get the pause.id in the togglePlay function when you're getting the play.id.
let pauseId = play.nextSibling.id


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...