为什么我的 FORM 元素有一个随机的 JQuery 属性? [英] Why does my FORM element have a random JQuery attribute?

查看:28
本文介绍了为什么我的 FORM 元素有一个随机的 JQuery 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 审批测试WatiN 来测试我的 ASP.NET MVC2 页面的集成.WatiN 启动 IE 以点击给定的 URL,然后在一个变量中给我浏览器的 html 响应.然后,批准测试允许我将 html 响应与 html 响应的批准"版本进行比较.除了某些东西(IE 或 JQuery)向我的元素添加意外属性外,该系统运行良好.

I'm using Approval Tests and WatiN to test the integration of my ASP.NET MVC2 pages. WatiN launches IE to hit the given URL and then gives me the browser's html response in a variable. Then, Approval Tests allow me to compare the html response with an "approved" version of the html response. This system works great except that something (either IE or JQuery) is adding an unexpected attribute to my element.

这是 IE 的 html 响应中表单标记的副本:

Here's a copy of the form tag from IE's html response:

<FORM method=post action=/Account/LogOn jQuery1314030136323="2">

注意表单元素中的 jQuery1314... 属性.它始终设置为2",但属性的名称始终不同(jQuery###########).由于每次都不一样,我的审批测试失败了.我需要在 html 输出上运行正则表达式并用蛮力删除参数,找到一种方法使属性名称每次都相同,或者完全删除.有什么想法吗?

Notice the jQuery1314... attribute in the form element. It is always set to "2", but the name of the attribute is always different (jQuery###########). Since it's different every time, my Approval Tests fail. I need to either run a regex on the html output and remove the param with brute force, find a way to make the attribute name the same every time, or remove is altogether. Any ideas?

我故意 - 不 - 用 ASP.NET 标记它,因为我真的认为这是特定于 IE 或 JQuery 的.

I'm intentionally -not- tagging this with ASP.NET because I really think this is specific to IE or JQuery.

推荐答案

这是 jQuery 添加到它与之交互的每个 DOM 元素的 uuid/jQuery.expando,以解决浏览器内存泄漏问题.

That's the uuid/jQuery.expando that jQuery adds to every DOM element it interacts with, to work around browser memory leaks.

旧样式代码等待 window.onunload 将 Javascript 数据与 DOM 标记解除绑定以防止内存泄漏.JQuery 通过在属性中使用一个简单的数字(如您的代码示例中的数字),然后在标签和数字的 Javascript 中保留一个哈希图(它称为 uuid)来避免这种情况.

Older style code waited for window.onunload to unbind Javascript data from DOM tags to prevent memory leaks. JQuery avoids this by using a simple number (like the one in your code example) in an attribute and then keeping a hashmap in Javascript of tags and numbers (which it calls the uuid).

奇怪的属性名称是jQuery.expando的值,你可以在代码中很容易地搜索到,每次都会看到它被设置为一个随机值.这样做是为了允许多个 jQuery 副本在页面上共存而不会相互干扰.

The weird attribute name is the value of jQuery.expando, which you can search for easily in the code and see it's set to a random value each time. This is done to allow multiple copies of jQuery to coexist on the page without interfering with each other.

我不知道在同一页面上需要多个 jQuery 的用例,我怀疑您也不需要此功能 - 您可以通过消除此轻松解决此问题特征.修改代码以将 jQuery.expando 设置为某个硬编码值,例如jquery",而不是随机数,您就可以开始了.

I'm not aware of a use case I've ever needed where I need more than one jQuery on the same page, and I suspect you don't need this functionality either - you could easily resolve this by just eliminating this feature. Modify the code to set jQuery.expando to some hard-coded value, like 'jquery', instead of a random number, and you're good to go.

注意不要在同一页面中使用两次 jQuery!尽管这样做不小心也会引入许多其他奇怪的副作用(例如 $ 的重用),因此这一点可能没有实际意义.

Be careful not to ever use jQuery twice in the same page though! Although doing so by accident introduces a lot of other strange side effects as well (like reuse of $), so that point may be moot.

我在这个问题中更详细地介绍了 jQuery.expando/uuid:为什么 JQuery 不公开其 UUID 功能?

I go into a little more detail about jQuery.expando/uuid in this question: Why Doesn't JQuery Expose its UUID Functionality?

您会在这篇文章中注意到,该属性的值是随机的——它是一个计数器,它基于到目前为止 jQuery 与多少标签进行了交互.如果你的代码要求属性值保持一致,你可能还是会遇到麻烦.

You'll notice in that write-up that the value of the attribute is random-ish - it's a counter based on how many tags have been interacted with by jQuery so far. If your code requires the attribute value to be consistent, you may still run into trouble.

更新

您需要修改您的 jquery 源代码.例如,1.6.2:http://code.jquery.com/jquery-1.6.2.js

You'll need to modify your jquery source. For example, 1.6.2: http://code.jquery.com/jquery-1.6.2.js

包括以下内容:

jQuery.extend({
    cache: {},

    // Please use with caution
    uuid: 0,

    // Unique for each copy of jQuery on the page
    // Non-digits removed to match rinlinejQuery
    expando: "jQuery" + ( jQuery.fn.jquery + Math.random() ).replace( /D/g, "" ),

您可以按如下方式更改 expando 行:

You can change the expando line as follows:

    // Does not support multiple copies of jQuery on the same page!
    // 0 included to match rinlinejQuery (/jQueryd+/)
    expando: "jQuery0",

这篇关于为什么我的 FORM 元素有一个随机的 JQuery 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆