考虑以下函数。每次调整浏览器大小以及每次我想将相同的初始值传递this.original_ul
给这个函数时都会调用它doLayout()
。它仅在初始化函数中单独计算一次。现在发生的事情是第二次调用它时,对$ul
变量的更改也正在反映 this.original_ul
。我怎样才能防止这种情况?
doLayout : function () {
alert('doLayout');
var size, $ul, $lis, loop, i;
size = this.computeElesPerCol();
$ul = this.original_ul; // I wanna preserve this between calls
$lis = $ul.children().filter(':gt(' + (size - 1) + ')');
loop = Math.ceil($lis.length / size);
i = 0;
$ul.css('float', 'left').wrap("<div style='overflow: hidden'></div>");
for (; i < loop; i = i + 1) {
$ul = $("<ul />").css('float', 'left').append($lis.slice(i * size, (i * size) + 4)).insertAfter($ul);
$ul.css("list-style","none");
}
}
编辑: :this
是对 javascript 对象 WebFloatLayout 的引用,它基本上根据每次调整浏览器大小时的可用高度进行多列布局。初始输入是<lis>
单个<ul>
. 这是它的初始化方式:
initialize:function () {
var webFloatLayout = this;
//call super parent initialize which will set the main dom element of component
ec.comp.Component.prototype.initialize.call(this);
var innerDivId = this.id + "_inner";
innerDivId = innerDivId.replace(new RegExp(ec.config.separator, "g"), "\\" + ec.config.separator);
$(document).ready(function() {
webFloatLayout.original_ul = $("#" + innerDivId).find(">:first-child");
webFloatLayout.original_ul.css("list-style","none");
webFloatLayout.layout();
});
},