轻松清除所有脚本设置变量(Javascript/jQuery)
我正在尝试找到一种简单的方法来重置 DOM 中的所有当前变量。我的应用程序处于“锁定”状态,但目前,当锁定时,如果您使用 Firebug 查看,最后一组信息在 DOM 中仍然可见。
我知道所有变量的所有名称,但我不想有一个包含所有变量(大约有 300 个)的非常麻烦的脚本,在其中我明确地清除它们的值。
有人知道是否有办法“清除”所有设置变量的 DOM?
如果这是一个愚蠢的问题,请提前道歉,并感谢您的回复。我的应用程序广泛使用 jQuery,但我所有的 Javascript/jQuery 搜索都显示为空白。
I'm trying to find a simple way to reset all current variables in the DOM. My app has a 'locked' state, but currently, when locked, the last set of information is still visible in the DOM if you look using Firebug for example.
I know all the names of all variables, but I don't want to have a really cumbersome script including all of them (there are approx. 300) where I explicitly clear their values.
Does anybody know if there's a way to 'purge' the DOM of all set vars?
Apologies in advance if this is a silly question, and thanks for any responses. My app uses jQuery extensively, but all my Javascript/jQuery searches have come up blank.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您将变量作为属性附加到 DOM 元素(您应该小心不要创建循环引用并触发 IE 内存泄漏;请参阅:crockford 文章)。
在关于泄漏的文章中,Crockford 先生提供了一个从所有 DOM 元素中清除函数引用的函数,这与您需要的类似:
他有
Replace with 逻辑来检查您的自定义属性/属性(在方便的地方保留一个列表)数组(如果您使用很多不同的属性),并确保您的属性名称没有与 src、title 等标准属性冲突)
Assuming that you're attaching variables to DOM elements as properties (which you should be careful not to create a cyclic reference and trigger IE memory leaks; see: crockford article).
In the article about leaks, Mr. Crockford gives a function for purging function references from all DOM elements, which is similar to what you need:
Where he has
Replace with logic that checks for your custom attributes/properties (keep a list handy in an array if you use a lot of different ones, and make sure that none of your property names conflict with standard attributes like src, title, etc.)
不要用全局变量扰乱 DOM,而是尝试在它们周围放置一个范围(使用函数)。
这里更好地解释: http://robertnyman.com/2008 /10/09/explaining-javascript-scope-and-closures/
Instead of cluttering the DOM with global variables, try putting a scope around them (using functions).
Better explained here: http://robertnyman.com/2008/10/09/explaining-javascript-scope-and-closures/
每个全局范围变量或函数实际上都是在当前窗口内定义的。您可以枚举对象的成员(请参阅这篇文章< /a>)。如果您可以分离用户定义的变量,那么您就拥有了所需的一切。也许您可以定义一个空窗口并比较这些变量。请参阅以下内容
Every global scope variable or function is actually defined within current window. You can enumerate members of an object (see this post). If you can separate user defined variables, then you have everything you need. May be you can define an empty window and compare those variables. See the following