发布于 2021-07-28 04:00:50
我真的不明白这个问题,但是您应该只对单选按钮使用单击事件,而不是对页面的每个div使用。这一行造成了不想要的行为:
jQuery("div").click(function(){
如果您的页面上还有其他div,然后单击它,您的函数将被触发。该函数只应在单击单选按钮时触发,您可以通过使用公共类和其他方式轻松地对这些按钮进行分组。那么您的函数应该如下所示:
$("div.commonClass").click(function(){
假设他们都有这个"commonClass“类。
发布于 2021-07-30 20:10:28
我真的不明白这个问题,但是您应该只对单选按钮使用单击事件,而不是对页面的每个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.
});
就这样!!对于这2行,您将始终只使用类ff_item_selected的选定项。在JQuery中编码时,$(this)关键字将对您有很大帮助。
https://stackoverflow.com/questions/68551469
复制相似问题