简体   繁体   English

JQuery - 仅适用于具有类的第一个元素的函数

[英]JQuery - Function applying only for first element with class

I'm trying to update a modal & video attributes on button click.我正在尝试在单击按钮时更新模式和视频属性。

It works well but the problem is that it only apply this when i click on the first button ( first element with the class videoBtn ), i want it to be applied on all other buttons with the same class它运作良好,但问题是它仅在我单击第一个按钮(具有类 videoBtn 的第一个元素)时应用它,我希望它应用于具有相同类的所有其他按钮

here is my code这是我的代码

$(".videoBtn").click(function(){
        $("#video").attr('src',$(".videoBtn").data('src'));
        $(".modal").attr('id', $(".videoBtn").data('target').substring(1));
      });

You are not using value of currently clicked element, but first with class, you should use $this instead of $('.videoBtn')您没有使用当前单击元素的值,但首先使用类,您应该使用$this而不是$('.videoBtn')

$(".videoBtn").click(function(){
   $("#video").attr('src',$(this).data('src'));
   $(".modal").attr('id', $(this).data('target').substring(1));
});
$(".videoBtn").each(function(){
  $(this).on('click', function() {
    $("#video").attr('src',$(".videoBtn").data('src'));
    $(".modal").attr('id', $(".videoBtn").data('target').substring(1));
  })
})

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

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