如何刷新存储和快照的jquery选择器变量 [英] How can I refresh a stored and snapshotted jquery selector variable
问题描述
这是一个带有testcase的jsfiddle:
我在昨天遇到一个jquery选择器的问题,我分配给一个变量, >- 将.elem分配给我的obj var
- 将两个长度记录到控制台。结果=> 4
- 将#3从DOM
- log obj删除到控制台=>删除的#3仍然存在,长度为仍然4.
我想出了jquery查询是快照?到变量,不能?不会?更新 - 将.elem更新到控制台.. yep结果=> 3而#3已经消失
- 现在我更新了.elem新宽度300
- logging obj& obj.width给我300 ..所以快照已经更新了?有趣的是,4个div中的3个具有新的宽度,但是删除的#3没有...
另一个测试:将一个li元素添加到domtree并记录obj和.elem。
.elem确实有新的li和obj没有,因为它仍然是旧的快照
有没有办法用新内容更新这个obj ?
我不想做一个新的obj,因为在我的应用程序中有很多信息保存在该对象中,我不想破坏...
是的,这是一个快照。此外,从页面DOM树中删除元素并不会神奇地消除对元素的所有引用。
您可以像这样刷新它:
var a = $(。elem);
a = $(a.selector);
迷你插件:
$。fn.refresh = function(){
return $(this.selector);
};
var a = $(。elem);
a = a.refresh();
虽然这个简单的解决方案并不适用于复杂的遍历。您将需要为 .selector
属性创建一个解析器来刷新那些的快照。
格式如下:
$(body)。find(div)。next(。sibling)。 prevAll()。siblings()。selector
//body div.next(.sibling).prevAll()。siblings()
就地小插件:
$。fn.refresh = function (){
var elems = $(this.selector);
this.splice(0,this.length);
this.push.apply(this,elems);
返回这个;
};
var a = $(。elem);
a.refresh()//无需任务
I ran yesterday in a problem with a jquery-selector I assigned to a variable and it's driving me mad.
Here is a jsfiddle with testcase:
- assign the .elem to my obj var
- log both lengths to the console. Result => 4
- Remove #3 from the DOM
- log obj to the console => the removed #3 is still there and the length is still 4. I figured out that jquery query is snapshotted? to the variable and can't?won't? be updated
- log .elem to the console.. yep Result => 3 and the #3 is gone
- Now I update .elem with a new width of 300
- logging obj & obj.width gives me 300.. So the snapshot has been updated ? What's interesting is that 3 of the 4 divs have the new width, but the removed #3 doesn't...
Another test: Adding a li element to the domtree and logging obj and .elem. .elem does have the new li and obj doesn't, because it's still the old snapshot
Is there no way to update this obj with the new content? I don't want to make a new obj, because in my application there is a lot information saved in that object, I don't want to destroy...
Yeah, it's a snapshot. Furthermore, removing an element from the page DOM tree isn't magically going to vanish all references to the element.
You can refresh it like so:
var a = $(".elem");
a = $(a.selector);
Mini-plugin:
$.fn.refresh = function() {
return $(this.selector);
};
var a = $(".elem");
a = a.refresh();
This simple solution doesn't work with complex traversals though. You are going to have to make a parser for the .selector
property to refresh the snapshot for those.
The format is like:
$("body").find("div").next(".sibling").prevAll().siblings().selector
//"body div.next(.sibling).prevAll().siblings()"
In-place mini-plugin:
$.fn.refresh = function() {
var elems = $(this.selector);
this.splice(0, this.length);
this.push.apply( this, elems );
return this;
};
var a = $(".elem");
a.refresh() //No assignment necessary
这篇关于如何刷新存储和快照的jquery选择器变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!