这行代码:
$('.resize').css({'width': new_size, 'height': new_size});
是不是做你认为它是。为了让对象在添加类时更改大小,您需要一个实际的CSS规则,该规则指定.resize
类,该规则不适用于动态计算的大小。
这行代码只是用.resize
类设置任何对象的高度和宽度,并且没有为没有该类的对象做任何事情。因此,你可以调整一次对象的大小(当它有这个类时)并且再也不会改变它的大小。它不会切换任何东西。
我建议你保存旧的尺寸,然后你可以在需要时恢复它,并保存切换值。 jQuery的。数据()函数工作得很好保存此类信息的:
$(document).ready(function() {
$(this).click(function() {
var this$ = $(this);
// if it's currently at the dynamic size, restore the original size
if (this$.data("dynamicallySized")) {
$(this.css(this$.data("origSize"))
.data("dynamicallySized", false); // set flag that we're not dynamic
} else {
// save original size
this$.data("origSize", {height: this$.height(), width: this$.width()})
.css(new_size)
.data(dynamicallySized", true); // set flag that we're dynamically sized
}
});
});
' toggleClass'是这个简单的方法。 – Starx 2012-03-06 07:04:28
如果toggleclass不工作可能你的css规则对于那个类不够具体 – charlietfl 2012-03-06 07:12:39
@charlietfl:我如何使它更具体? – 2012-03-06 07:15:22