获取每个字段集的动态类

时间:2011-09-17 06:38:23

标签: javascript jquery

如何获取(动态)包含元素的每个类fieldset

像:

如果点击按钮adult = get class =&gt;来自html的.adult <fieldset class="adult"> 如果点击按钮child wbed = get class =&gt;来自html的.child wbed <fieldset class="child wbed"> 如果点击按钮child bed = get class =&gt;来自html的.child bed <fieldset class="child bed"> 如果点击按钮baby = get class =&gt; h {.baby

中的<fieldset class="baby">来自此处$('button').live("click", function (event) { event.preventDefault(); $(this).remove(); var $this = $(this), $div = $this.parent('fieldset'); var name = '.' + $(this).closest('fieldset').attr("class"); alert(name); });

示例: http://jsfiddle.net/WErW9/7/

{{1}}

1 个答案:

答案 0 :(得分:1)

如何使用closest()

$('button').live("click", function(event){
    event.preventDefault();
    var $this = $(this), $div = $this.closest('fieldset');
    var name = '.' + $div.attr("class");
    alert(name);
});

[View output]