簡體   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