Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 内联onclick函数(this)不调用jQuery方法_Javascript_Jquery_Html_Event Handling - Fatal编程技术网

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

Javascript 内联onclick函数(this)不调用jQuery方法,javascript,jquery,html,event-handling,Javascript,Jquery,Html,Event Handling,我在处理onclick事件时遇到了麻烦。 我的HTML: 问题是音频文件没有播放。 我还做了一些测试 function sound(element){ alert(element.tagName); -> it worked alert(element.parent().tagName); -> not worked and so on with siblings() or next() 一定是出了什么事。请帮帮我。非常感谢。代替 function sound(elem

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

问题是音频文件没有播放。 我还做了一些测试

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');
}

element
(您正在传递的
this
)是本机DOM元素,而不是jQuery包装器!最好指出,
onclick=”“
是一种过时的添加事件处理程序的方法。
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');
}