2017-07-26 77 views
0

這看起來像一個基本的問題,但未能找到答案..有什麼我需要添加的按鈕,使用戶到主頁和播放聲音?目前它只播放聲音。我希望它立即播放聲音,然後進入主頁。爲什麼我的按鈕不能播放我的聲音並轉到鏈接?

function playBeep() { 
 
    var sound = document.getElementById("Sound") 
 
    sound.play() 
 
}
<div class="animatedButton"> 
 

 
    <audio id="Sound" src="https://www.soundjay.com/button/sounds/beep-07.mp3"></audio> 
 

 
    <a href="Home.html"> </a> 
 
    <button id="aButton" onclick="playBeep()"> 
 
    <img src="img/Home.png"> 
 
    </button> 
 

 
</div>

+0

你在哪裏告訴瀏覽器導航到一個新的頁面?如果您立即更改頁面,用戶將聽不到聲音。 – PeterMader

+0

我已經創建了用戶點擊進入主頁按鈕的按鈕,animatedButton是應該播放聲音然後鏈接的按鈕。 –

回答

1

你應該等到<audio>元素播放完畢,然後告訴瀏覽器導航到一個新的頁面:

function playBeep() { 
 
    var sound = document.getElementById("Sound"); 
 
    sound.play(); 
 
    sound.addEventListener('ended', function() { 
 
    location.href = 'Home.html'; 
 
    }); 
 
}
<div class="animatedButton"> 
 

 
    <audio id="Sound" src="https://www.soundjay.com/button/sounds/beep-07.mp3"></audio> 
 
    <button id="aButton" onclick="playBeep()"> 
 
    <img src="img/Home.png"> 
 
    </button> 
 

 
</div>

+0

這似乎是最簡單最有效的答案,也幫助我刪除標籤,因爲某些原因,您不應該在標籤上環繞按鈕。感謝您的幫助! –

0

剛需添加您的鏈接window.location = "http://stackoverflow.com";

​​

0

function playBeep() { 
 
    var sound = document.getElementById("Sound") 
 
    sound.play(); 
 
    window.location.href = '/'; //Go to HomePage 
 

 
}
<div class="animatedButton"> 
 

 
    <audio id="Sound" src="https://www.soundjay.com/button/sounds/beep-07.mp3"></audio> 
 

 
    <a href="Home.html"> </a> 
 
    <button id="aButton" onclick="playBeep()"> 
 
    <img src="img/Home.png"> 
 
    </button> 
 

 
</div>

0

<a href="Home.html"> </a>

您的按鈕是不是在<a>標籤 也許這將工作:

<a href="Home.html"> 
    <button id="aButton" onclick="playBeep()"> 
    <img src="img/Home.png"> 
    </button> 
</a> 
0

你的問題是一個結束標記。 標籤「a」必須包裹整個按鈕。

<a href="Home.html"> 
    <button id="aButton" onclick="playBeep()"> 
    <img src="img/Home.png"> 
    </button> 
</a> 
0

您可以隨時使用jQuery來偵聽聲音以結束並重定向用戶。

/* Bind Ended Event to Sound */ 
 
$('#Sound').on('ended', function(){ 
 
    console.log('User Redirected'); 
 
}); 
 

 
/* Bind Click Event to Button */ 
 
$('#aButton').on('click', function() { 
 
    $('#Sound')[0].play(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="animatedButton"> 
 
    <audio id="Sound" src="https://www.soundjay.com/button/sounds/beep-07.mp3"></audio> 
 
    <a href="Home.html"></a> 
 
    <button id="aButton"> 
 
     <img src="http://findicons.com/files/icons/1580/devine_icons_part_2/128/home.png"> 
 
    </button> 
 
</div>

或者你可以只使用JavaScript監聽聲音結束並重定向用戶。

/* Variable Defaults */ 
 
var sound = document.getElementById('Sound'); 
 
var button = document.getElementById('aButton'); 
 

 
/* Bind Ended Event to Sound */ 
 
sound.addEventListener('ended', function(){ 
 
    console.log('User Redirected'); 
 
}); 
 

 
/* Bind Click Event to Button */ 
 
button.addEventListener('click', function() { 
 
    sound.play(); 
 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div class="animatedButton"> 
 
    <audio id="Sound" src="https://www.soundjay.com/button/sounds/beep-07.mp3"></audio> 
 
    <a href="Home.html"></a> 
 
    <button id="aButton"> 
 
     <img src="http://findicons.com/files/icons/1580/devine_icons_part_2/128/home.png"> 
 
    </button> 
 
</div>

相關問題