多个元素具有相同的类名,但在纯jQuery中独立执行事件 [英] Same class name for multiple elements but execute event independently in pure jQuery

查看:104
本文介绍了多个元素具有相同的类名,但在纯jQuery中独立执行事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我想知道是否可以按目标按钮,这只会影响该按钮,无论他们是否具有相同的班级名称,我都不想按目标按钮

按钮,并在所有类上执行附加到该类名称的事件,我只想针对每个目标按钮独立地针对该目标按钮执行一个事件,而不是通过一个按钮对所有目标按钮执行一个事件.我想知道如何在jQuery中做到这一点

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
  $('.x').click(function(){
  $('.x').css({'background-color': 'red'})
});
}); 
</script>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button> 

解决方案

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
  $('.x').click(function(){
  $(this).css({'background-color': 'red'})
});
}); 
</script>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button> 

Hello guys I want to know if there is a way were I can press on a targeted button which will only affect that button only regardless if they have the same class name and I don't want to press on a targeted

button and that event that is attach to that class name is executed on all of them I only want to execute an event for that targeted button independently on each of them not on all of them by one button. I was wondering how I can do this in jQuery

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
  $('.x').click(function(){
  $('.x').css({'background-color': 'red'})
});
}); 
</script>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button>

解决方案

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
  $('.x').click(function(){
  $(this).css({'background-color': 'red'})
});
}); 
</script>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button>
<button class='x'>Execute</button>

这篇关于多个元素具有相同的类名,但在纯jQuery中独立执行事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆