我想删除类从其他单选按钮,当我点击单选按钮在另一个部分与jQuery(用于氧生成器wordpress)。
jQuery(document).ready(function(){
jQuery("div").click(function(){
jQuery("div.volba-1-1 div, div.volba-1-2 div, div.volba-1-3 div").removeClass("ff_item_selected");
});
});
这对我来说很好,但如果我点击其他地方的单选按钮仍然被选中,但类被删除。有人能帮我一下吗?
网站查看我的问题:在这里输入链接描述
图像描述
感谢我真的不明白这个问题,但是你应该只对单选按钮使用click事件,而不是对页面的每个div都使用。这一行会导致不期望的行为:
jQuery("div").click(function(){
如果您的页面上有任何其他div并且您单击它,您的函数将被触发。该函数应该只在单击单选按钮时触发,您可以通过在其他方式中使用公共类轻松地对其进行分组。然后你的函数应该是这样的:
$("div.commonClass").click(function(){
假设它们都有这个"commonClass"类。
我真的不明白这个问题,但是你应该只对单选按钮使用click事件,而不是对页面的每个div都使用。这一行会导致不期望的行为:
jQuery("div").click(function(){
如果您的页面上有任何其他div并且您单击它,您的函数将被触发。该函数应该只在单击单选按钮时触发,您可以通过在其他方式中使用公共类轻松地对其进行分组。然后你的函数应该是这样的:
$("div.commonClass").click(function(){
假设它们都有这个"commonClass"类。
编辑:
首先,您需要将所需的div成员设置为一个公共类:
<div class="commonClass someOtherClass"> //first div
<div class="commonClass"> //2nd div
//and so on!! just add "commonClass" to these div's
然后在JQuery代码中:
$("div.commonClass").click(function(){
$("div.commonClass").removeClass("ff_item_selected");
//this line will remove ff_item_selected class from all your divs (the ones who are members of commonClass)
$(this).addClass("ff_item_selected");
//this line will add the ff_item_selected class only to the item that was clicked.
});
就是这样!!有了这两行,您将始终只有您选择的项目与类ff_item_selected。$(this)关键字将帮助你在JQuery中编码。