jquery siblings属性为数组

时间:2011-06-05 11:25:04

标签: jquery attr siblings

任何人都知道如何从所有兄弟姐妹(单选按钮组的标签)中获取属性'rel'的值到数组中?

我尝试过类似的东西,但显然不起作用:

<label rel="option_local" class="type_radio" for="type_3">
    <input type="radio" checked="checked" value="3" id="type_3" name="type" /> 
Local</label>
<label rel="option_remote" class="type_radio" for="type_2">
    <input type="radio" value="2" id="type_2" name="type" /> 
Remote</label>
<label rel="option_html" class="type_radio" for="type_1">
    <input type="radio" value="1" id="type_1" name="type" /> 
HTML</label>


$('.type_radio').click(function() {
    var r = $(this).attr('rel');
    var arr = $(this).siblings().attr('rel');   
    $.each(arr, function(k, v) {
        $('.' + v).addClass('dn');
    });
    $('.' + r).removeClass('dn');
    return false;
});

我正在尝试使用类循环遍历元素,这些类引用特定触发器的'rel'属性并从中删除特定类。然后将此类应用于引用所选类的类。

所有标签都有'type_radio'类。

1 个答案:

答案 0 :(得分:1)

好的 - 我已经成功实现了它 - 这就是:

$('.type_radio').click(function() {
    var r = $(this).attr('rel');
    var arr = $(this).siblings();   
    var tr;
    $.each(arr, function() {
        tr = $(this).attr('rel');
        $('.' + tr).addClass('dn');
    });
    $('.' + r).removeClass('dn');
});