jquery ui添加不必要的代码并破坏事物

时间:2013-05-10 20:14:00

标签: javascript jquery

好的,我一直在努力使用本地存储来保存已调整大小的元素

当你加载页面时,ui代码被添加到resize div。 首先,您可以调整元素的大小,但如果您保存它并刷新页面,则会再次添加调整大小的ui代码,这会破坏它。我知道我可以在保存之前运行替换,但我想知道是否还有其他更有效的方法。

代码不止一次添加。
http://jsfiddle.net/code_cookies/Spkzq/95/

<div class="ui-resizable-handle ui-resizable-e" style="z-index: 1000;"></div>
<div class="ui-resizable-handle ui-resizable-s" style="z-index: 1000;"></div>
<div class="ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se"     style="z-index: 1000;"></div>

JS:

if (localStorage.pins === "") { 
    $('#draggable').html('<div id="image"class="resize" style="padding:20px; width:200px; height:200px; background-color:white;"><img class="" width="100%" height="100%" src="http://i48.tinypic.com/31368e9.jpg"/></div>');
}
else { 
    $('#draggable').html(localStorage.pins);
}
$("#image").resizable();
$('#save').click(function() {
    localStorage.pins = $('#draggable').html();
});

1 个答案:

答案 0 :(得分:3)

您可以通过调用destroy删除jquery UI添加的所有内容:

$("#image").resizable( "destroy" );