传递Jquery对象的功能不适用于IMG

Vis*_*hnu 2 html javascript jquery

我想将Jquery对象传递给函数,但它不适用于IMG标记.

我在下面做了一个例子.当我点击文本时它可以工作,但是当我点击图像时它不起作用.

$(document).on('click','.playvid',function (event) {
    event.stopPropagation();
    popup($(event.target));
}); 
function popup(data)
{
    data.html("success");   
}
Run Code Online (Sandbox Code Playgroud)
.playvid 
{
    cursor:pointer;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div data-id="3ZdHRvPNyCI" class=" playvid" ><img src="https://cdn4.iconfinder.com/data/icons/iconset-addictive-flavour/png/button_green_play.png">WATCH DEMO</div>
Run Code Online (Sandbox Code Playgroud)

Ror*_*san 5

问题是因为,根据您单击的位置,event.target可以是img不能包含的元素html.相反,传递$(this)给你的函数,因为它将包含.playvid元素:

$(document).on('click', '.playvid', function(event) {
  event.stopPropagation();
  popup($(this));
});

function popup(data) {
  data.html("success");
}
Run Code Online (Sandbox Code Playgroud)
.playvid {
  cursor: pointer;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<div data-id="3ZdHRvPNyCI" class="playvid">
  <img src="https://cdn4.iconfinder.com/data/icons/iconset-addictive-flavour/png/button_green_play.png">
  WATCH DEMO
</div>
Run Code Online (Sandbox Code Playgroud)