需要帮助将功能添加到html中的按钮

时间:2020-02-03 13:36:22

标签: html

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .in-middle {
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .button {
            border-radius: 50%;
            background-color: red;
            color: white;
        }
    </style>
</head>

<body>
    <div class="in-middle">
        <button class="button">
            click me
        </button>
    </div>
</body>

</html>

基本上,我有一个按钮的代码。如何添加一个功能,使其在隐藏控制器的情况下开始播放音乐,并在仍播放音乐的同时将您发送到下一页?

1 个答案:

答案 0 :(得分:1)

您必须先获取音频源,然后在按钮上分配功能

<audio id="myAudio">
  <source src="horse.ogg" type="audio/ogg">
</audio> 
<div id="controler">

 </div>
<div class="in-middle">
            <button class="button" onclick="myfunction">
                click me
            </button>
  </div>

然后将功能添加到js文件中

function myfunction(){
    document.getElementById("myAudio").play(); //it  will start the audio
    document.getElementById("controler").style.display="none" // it will hide your controller
    window.location.href="pageurl"; // this will open new window
}
相关问题