简体   繁体   English

jQuery从许多具有相同.class的元素中更改了Clicked元素的CSS背景

[英]jQuery change CSS background of clicked element from many elements with same .class

I'm working on a simple jQuery script that i want to change the background color of the clicked element. 我正在研究一个简单的jQuery脚本,我想更改clicked元素的背景颜色。

So here is my code: 所以这是我的代码:

<dt>
<input id="p_method_banktransfer" value="banktransfer" type="radio" name="payment[method]" title="Bank Payment"  class="radio validation-passed" autocomplete="off"/>ev
<label for="p_method_banktransfer">Банков превод </label>
</dt>

I have many <dt>...</dt> tags 我有很多<dt>...</dt>标签

Here is the CSS of <dt> : 这是<dt>的CSS:

.opc-wrapper-opc .payment-block dt { 
background: #3399CC;
border-radius: 2px;
margin: 0px 0px 13px;
position: relative;
}

So actually it seems that i am clicking on the label or on the input but the background that i want to change is of that dt element. 所以实际上似乎我在单击标签或输入,但是我要更改的背景是该dt元素的。

How i can make it ? 我该怎么做?

$('body').on('click', 'dt', function(){
  $(this).css('background','red'); // substitute your color for red
});

UPDATE 更新

To change all <dt/> OTHER than the clicked one back to the original color: 要将所有<dt/> OTHER(单击的内容除外)更改回原始颜色,请执行以下操作:

$('body').on('click', 'dt', function(){
  $('dt').not(this).css('background', '#3399CC'); // change back to original color
  $(this).css('background','red'); // substitute your color for red
});

暂无
暂无

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

相关问题 使用jQuery更改单击元素的样式并将该样式从具有相同类的其他元素中删除 - Using jQuery to change style of clicked element and removing that style from other elements with same class 对于具有相同类的多个元素,仅影响jquery中的单击元素 - affect only the clicked element in jquery for multiple elements with same class jQuery - 仅针对单击元素进行更改,而不是所有具有相同类的更改 - jQuery - change for only clicked element, not all with the same class 在jQuery中,如何测试某个元素是否是同一类的许多元素中的第一个? - In jQuery, how to test whether an element is the first of many elements of same class? jQuery / CSS-将鼠标悬停在具有相同href的元素上更改CSS类 - jQuery/CSS - Change css class on hover on elements with same href 如何使用jQuery从具有相同类名的许多元素中选择一个特定元素(div)? - How to select one specific element (div) from many elements with same class name in with jQuery? 如何更改与许多其他元素共享其 class 的单击元素的属性 - How do I change the property of a clicked element which shares its class with many other elements 使用JQuery更改同一类中所有元素的CSS样式 - Use JQuery to change all element's css style in same class jQuery:如果单击相同的元素,则删除类 - jQuery: Remove class if the same element is clicked jQuery针对许多来自同一类的元素 - JQuery targeting one element from many with same class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM