如何获取(动态)包含元素的每个类fieldset
?
像:
如果点击按钮adult
= get class =>来自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}}
答案 0 :(得分:1)
如何使用closest()
:
$('button').live("click", function(event){
event.preventDefault();
var $this = $(this), $div = $this.closest('fieldset');
var name = '.' + $div.attr("class");
alert(name);
});