【问题标题】:RemoveClass of other childeren in same parent div同一父 div 中其他子项的 RemoveClass
【发布时间】:2012-11-28 08:37:38
【问题描述】:

我正在尝试用 div 替换我的广播圈。

通过单击 div 进行选择。 所选 div 的突出显示有效。

但是当我选择其他选项之一时,如何删除同一父项中其他子项的“选定”类?

我的 JS 现在是:

$('.option').click(function() {
    $('input', this).prop("checked", true);
    $(this).addClass('optionSelected');
}

http://jsfiddle.net/S7Js8/

【问题讨论】:

  • 您使用的是什么版本的 CSS?这在 CSS3 中要简单得多,但如果您在 IE9 之前需要向后兼容,它就行不通了。无论哪种方式,我都可以为您指明方向。

标签: jquery css html radio


【解决方案1】:

使用siblings() 定位同级div,

$('.option').click(function() {
    $('input', this).prop("checked", true);
    $(this).addClass('optionSelected')
           .siblings()
           .removeClass('optionSelected');
});​

FIDDLE

【讨论】:

  • 谢谢!对我有用 :) 我几乎可以肯定有“兄弟姐妹”之类的东西,但不知道该谷歌搜索什么!哈哈!
  • @PowerGuitarist - jQuery 有一种命名方式!如果对您有帮助,请记住接受答案。
【解决方案2】:

试试这个,

$('.option').click(function() {
    $(this).parent().find('.option').removeClass('optionSelected');
    $('input', this).prop("checked", true);
    $(this).addClass('optionSelected');
});

如果所有选项都是兄弟,那么你可以这样做,

$('.option').click(function() {
    $(this).siblings('.option').removeClass('optionSelected');
    $('input', this).prop("checked", true);
    $(this).addClass('optionSelected');
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多