用Ajax在Hover上播放声音

时间:2012-05-22 20:54:35

标签: javascript jquery audio html5-audio

我试图在悬停时播放通过Ajax加载的内容,基于此处的“重叠”方法:http://css-tricks.com/play-sound-on-hover/

function audioController(){
$("a") // loop each menu item
  .each(function(i) {
    if (i != 0) { // only clone if more than one needed
      $("#hover")
        .clone()
        .attr("id", "hover-" + i)
        .appendTo($(this).parent());
    }
    $(this).data("beeper", i); // save reference
  })
  .mouseenter(function() {
    $("#hover-" + $(this).data("beeper"))[0].play();
  });
$("#hover").attr("id", "hover-0"); // get first one into naming convention
}

这适用于所有非ajax href。但是,对于Ajax内容,它仅适用于ajax内容中的第一个href。我收到此错误消息:

Uncaught TypeError: Cannot call method 'play' of undefined 这就是这一行:

$("#hover-" + $(this).data("beeper"))[0].play();

以下是我尝试使用Ajax调用加载函数的方法:

    $("#site-nav .nav1").on("click", function(event) {
    $('ul.top-level').load('assets/includes/recent.php', function(){
        $(this).show();
        audioController();
    });
    event.preventDefault();
});

1 个答案:

答案 0 :(得分:1)

我建议您尝试使用SoundManager2 for HTML5。

你可以找到带有样本的here

相关问题