JQuery addclass to selected div,如果选择了另一个div,则删除类 [英] JQuery addclass to selected div, remove class if another div is selected

查看:124
本文介绍了JQuery addclass to selected div,如果选择了另一个div,则删除类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个formbuilder,我想改变一个标题div的外观。
当点击它应该得到一个边框,但是当另一个动态生成的div被点击时,类应该被删除,第二个点击的div必须得到.active类。



无论如何,我发现一些工作,但我仍然需要如果另一个div被选中,上一个div.removeclass和选择的div .addclass



这样做:

  / *添加类* 
$(document).ready(function(){
$(document).on('click','.HeadingDiv',function(){/ * this'.HeadingDiv'我需要一些动态的* /
$('HeadingDiv')。removeClass('active'); / *这个'.HeadingDiv'可以是任何东西,我需要一个动态的这里* /
$ this).addClass('active');
});
});


解决方案

你看起来像这样短而有效: / p>

http://jsfiddle.net/XBfMV/

  $('div')。on('click',function(){
$('div')。removeClass活动');
$(this).addClass('active');
});

您可以简单地为所选的div添加一个普通类'active'。当点击div时,删除活动类,并将其添加到点击的div。


I'm making a formbuilder, I would like to change the appearance of for example a heading div. When clicked it should get a border but when another dynamically generated div is clicked the class should be removed and the second clicked div has to get the .active class.

How can I do this with generated divs?

Anyway I found something that works but I still need the If another div is selected, previous div.removeclass and selected div.addclass

This works:

/* Add Class */
$(document).ready(function() {
    $( document ).on( 'click', '.HeadingDiv', function () { /* This '.HeadingDiv' could be anything, I need something dynamic here */
        $('.HeadingDiv').removeClass('active'); /* This '.HeadingDiv' could be anything, I need something dynamic here */
        $(this).addClass('active');
    });
});

解决方案

Are you looking something like this short and effective:

http://jsfiddle.net/XBfMV/

$('div').on('click',function(){
  $('div').removeClass('active');
  $(this).addClass('active');
});

you can simply add a general class 'active' for selected div. when a div is clicked, remove the 'active' class, and add it to the clicked div.

这篇关于JQuery addclass to selected div,如果选择了另一个div,则删除类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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