检查下面的代码:
<script>
var cls = ['', 'a', 'a b', 'a b c'];
var divs = $('div.wrap').children();
请注意,children() 执行破坏性操作,即它构造一个新的 jquery 对象
var appendClass = function() {
divs.append(function() {
return '<div>' + (this.className || 'none') + '</div>';
});
};
appendClass();
$('button').bind('click', function() {
var tc = this.className || undefined;
divs.toggleClass(tc);
appendClass();
});
$('a').bind('click', function(event) {
event.preventDefault();
//(children is a destructive method which have constructed new jquery object so what is the use of using empty method on the upcoming statement)
divs.empty().each(function(i) {
this.className = cls[i];
});
appendClass();
});
那么在div变量上使用空方法有什么用?