html - 我如何在按钮菜单上添加声音

标签 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;
}

这是我的代码,没有任何声音,我怎样才能在上面输入声音?

最佳答案

你可以使用这个 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();
        });
    });

和 Controller 的 HTML:

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

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

下次请使用搜索:

Play an audio file using jQuery when a button is clicked

关于html - 我如何在按钮菜单上添加声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29043026/

相关文章:

css - 如何从 css Selenium Webdriver 获取后台 url

video - 我应该在jar文件的manifest.mf文件中给出哪个主类,该jar文件中有一个没有主类的applet?

jquery - 缓存为空时获取音频文件的持续时间属性的问题

jquery - Html 5 Audio 在 iphone safari 浏览器中不起作用

java - 在 JLabel 中使用 HTML 创建一个栏

javascript - 如果在表内绑定(bind),则使用 knockout

php - 在文本区域中输入 MS-Word 方程

css - 线性渐变仅适用于 -moz 供应商前缀

jquery - 使用 jQuery 将网页调整为 facebook 选项卡

html - 使用CSS在div标签中重叠按钮