我如何在按钮菜单上发出声音

时间:2015-03-13 22:29:48

标签: html css audio onclick

HTML:

<!DOCTYPE html>
<head>
<meta charset="UTF-8">
</head>

<body>
<div class="buttons" >
<li><pre><strong><a href="home.html">Home</a></strong> </pre></li>
</div>
</body>

CSS:

.buttons{
 font-size:16px;

position:fixed;
bottom:217px;
left:183px;
}

这是我的代码没有任何声音,我怎么能输入声音呢?

1 个答案:

答案 0 :(得分:1)

你可以使用这个jQuery代码:

  $(document).ready(function() {
        var audioElement = document.createElement('audio');
        audioElement.setAttribute('src', 'audio.mp3');
        audioElement.setAttribute('autoplay', 'autoplay');
        //audioElement.load()

        $.get();

        audioElement.addEventListener("load", function() {
            audioElement.play();
        }, true);

        $('.play').click(function() {
            audioElement.play();
        });

        $('.pause').click(function() {
            audioElement.pause();
        });
    });

控制器的HTML:

<div class="play">Play</div>

<div class="pause">Stop</div>

下次请使用搜索

Play an audio file using jQuery when a button is clicked