如何为 jQuery 对象分配属性?
据我了解,通过美元符号引用 jQuery 中的任何 DOM 元素(如下所示:$("#mydiv")
)都会返回一个对象。
我希望向对象添加一个附加属性:
$("#mydiv").myprop = "some value";
但这似乎不起作用。 我正在尝试在该对象中存储一个值,以便稍后可以引用它。
不过,即使在设置属性后,以下代码也会立即给出一个 undefined
值。
$("#mydiv").myprop = "some value";
alert($("#mydiv").myprop);
此代码也不起作用:
$("#mydiv")["myprop"] = "some value";
alert($("#mydiv").myprop);
有办法实现此目的吗? 或者我是否需要通过 $("#mydiv").attr("myprop", "some value") 将所有属性存储为 DOM 对象上的属性(这是不太理想的)。
谢谢!
It is in my understanding that referencing any DOM element in jQuery via the dollar sign (like this: $("#mydiv")
), returns an object.
I am looking to add an additional property to an object as such:
$("#mydiv").myprop = "some value";
but this does not seem to work.
I am trying to store a value in this object so I can reference it later.
The following code, though, gives me an undefined
value even immediately after I set the property.
$("#mydiv").myprop = "some value";
alert($("#mydiv").myprop);
this code does not work either:
$("#mydiv")["myprop"] = "some value";
alert($("#mydiv").myprop);
Is there a way to achieve this? Or do I need to store all my properties as attributes on the DOM object via $("#mydiv").attr("myprop", "some value")
(this is much less desirable).
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jQuery 通过
data
公开了属性包的概念方法。这避免了 DOM 操作,这在性能方面可能会很昂贵。
jQuery exposes the concept of a property-bag with the
data
method.This avoids DOM manipulations which can be expensive in terms of performance.
$ 函数创建一个代表 dom 元素的 jquery 对象。 您可以更改对象的属性,但这些更改不会在它“代表”的元素中显示出来,除非 jquery 知道如何处理它们(也许分配一个单击事件处理程序)。
因此,当您进行第一个选择时,它会修改您创建的对象,但不会对实际的 html 元素执行任何操作。 当您再次运行 $ 时,它会创建一个单独的对象,该对象不会保留您对第一个对象所做的属性更改。
您也许可以直接更改 html 对象:(没有 jquery)
getElementByID("theid").myprop = "hello world";
结果是:
但设置 jquery 对象的属性是行不通的。 要在 jquery 中获得等效效果,请使用 Ken Browning 建议的 .data 方法。 (又太晚了...):D
The $ function creates a jquery object that represent a dom element. You can change the attributes of the object, but those changes won't be made aparent in the element it "represents" unless jquery knows what to do with them (assigning a click event handler perhaps).
So when you make the first selection, it modifies the object you made, but doesn't do anything to the actual html element. When you run $ again, it creates a SEPARATE object, that does not retain the attribute change you made to the first.
you may be able to make a direct change to an html object: (without jquery)
getElementByID("theid").myprop = "hello world";
resulting in:
<p id="theid" myprop="hello world"></p>
but setting an attribute of a jquery object just won't do it. To get the equivalent effect in jquery, use the .data method that Ken Browning suggested. (too late again...) :D我不确定这是否是由于 jQuery 的更改所致。 但我已经按照问题中的描述完成了...
现在就我而言,我必须在稍后的方法调用中使用这个对象:
然后该方法可以这样做:
正如我所说 - 这可能是一个更新在 jQuery 引擎中,但似乎不再需要
.data()
调用:-)I'm not sure wether or not this is due to a change in jQuery. But I've done it pretty much as described in the question...
Now in my case I have to use this object in a method call later on:
The method can then do like this:
As I said - it might be an update in the jQuery engine, but it seems that the
.data()
call is no longer needed :-)