0

ラジオサークルをdivに置き換えようとしています。

divをクリックして選択すると機能します。選択したdivの強調表示が機能します。

しかし、他のオプションの1つを選択したときに、同じ親の他の子の「選択された」クラスを削除するにはどうすればよいですか?

私のJSは今:

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

http://jsfiddle.net/S7Js8/

4

2 に答える 2

2

siblings()兄弟のdivをターゲットにするために使用します。

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

フィドル

于 2012-12-10T21:10:51.063 に答える
0

これを試して、

$('.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');
});
于 2012-12-10T21:13:12.257 に答える