繁体   English   中英

内联onclick函数(this)不调用jQuery方法

[英]inline onclick function(this) not calling jQuery methods

处理onclick事件时遇到麻烦。 我的HTML:

<div>
  <button onclick="sound(this)">Listen</button> 
  <audio class="audio" src="example.mp3"></audio>
</div>

我的JS(jQuery):

function sound(element){
  $audio = element.siblings('.audio').first();
  $audio.trigger('play');
}

问题是音频文件未播放。 我也做了一些测试。

function sound(element){
   alert(element.tagName); -> it worked
   alert(element.parent().tagName); -> not worked and so on with siblings() or next()

一定有问题。 请帮帮我。 非常感谢。

代替

function sound(element){
  $audio = element.siblings('.audio').first();
  $audio.trigger('play');
}

尝试这个

function sound(element){
  $audio = $(element).siblings('.audio').first(); //here 'element' is just a DOM element you have to wrap 'element' in jquery wrapper.
  $audio.trigger('play');
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM