jQuery没有';将类添加到HTML元素
当尝试将类('hidden')添加到jQuery没有';将类添加到HTML元素,jquery,twitter-bootstrap,Jquery,Twitter Bootstrap,当尝试将类('hidden')添加到$(此)时,它似乎将其添加到jQuery对象[textarea.form-control.hidden,prevObject:n.fn.init[1],context:button.btn.btn-info.btn-flat.hidden,选择器:“textarea”],但不添加到实际的HTML元素。我错过了什么 这是因为您已经在main div.item上调用了函数。因此,当您单击按钮时,.item上的上述函数将再次调用,因为按钮是.item的子项 使用e.
$(此)
时,它似乎将其添加到jQuery对象[textarea.form-control.hidden,prevObject:n.fn.init[1],context:button.btn.btn-info.btn-flat.hidden,选择器:“textarea”]
,但不添加到实际的HTML元素。我错过了什么
这是因为您已经在main div.item上调用了函数。因此,当您单击按钮时,.item上的上述函数将再次调用,因为按钮是
.item
的子项
使用e.stopPropagation()
停止从父div调用函数
$('.item button').click(function(e) {
e.stopPropagation();
$parent = $(this).parent().parent();
$parent.find('span').removeClass('hidden');
$parent.find('textarea').addClass('hidden');
$parent.find('button').addClass('hidden');
console.log($parent.find('textarea'));
})
fiddle:这是因为您已经在main div.item'上调用了函数,所以当您单击按钮时,.item'上的上述函数将再次调用,因为按钮是
的子项
使用e.stopPropagation()
停止从父div调用函数
$('.item button').click(function(e) {
e.stopPropagation();
$parent = $(this).parent().parent();
$parent.find('span').removeClass('hidden');
$parent.find('textarea').addClass('hidden');
$parent.find('button').addClass('hidden');
console.log($parent.find('textarea'));
})
小提琴: