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

查看:90
本文介绍了为什么我的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输出上运行一个正则表达式并用暴力删除param,找到一种方法使每次属性名称相同,或者完全删除。有什么想法吗?

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解除DOM标记中的Javascript数据以防止内存泄漏。 JQuery通过在属性中使用一个简单的数字(比如代码示例中的那个)来避免这种情况,然后在标签和数字的Javascript中保留一个hashmap(它称之为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.

Be注意不要在同一页面中两次使用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的详细信息:
为什么不' t JQuery暴露其UUID功能?

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

你会注意到该文章的属性值是random-ish - 它是一个基于计数器的到目前为止,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 (/jQuery\d+/)
    expando: "jQuery0",

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

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