从jQuery对象派生HTMLElement对象 [英] Deriving an HTMLElement Object from jQuery Object
问题描述
我正在做一个相当详尽的DOM操作系列,其中一些元素(特别是表单元素)有一些事件。
我动态创建一个源元素)多个< select>
框并为其分配change()事件。
事件执行,并且在事件的上下文中,this是HTML元素对象。
我现在需要做的是确定一个上下文HTML元素对象。我有这些对象作为jQuery实体存储在已排序的数组中,但很明显
[HTMLElement Object]!= [Object Object]
而诀窍是我不能强制转换$(this)并进行有效的比较,因为这将创建一个新的对象,指针将不同。 / p>
所以...我一直在敲我的头一会儿。在过去,我已经能够通过做一个innerHTML比较规避这个问题,但在这种情况下,我比较的对象是100%完全相同,只是有很多。因此,我需要一个坚实的比较。
这将是很容易,如果我能以某种方式从我的原生jQuery对象导出HTMLElement对象。
想法,其他想法?帮帮我。 :(
不能使用 $(this).data
以保存您的元素的数据,然后检查值(以后假设你不能只是给这些东西纯粹的id值。)
还有jQuery本身有一个guid元素,你可以使用(注意!)
$ myNewObject).data(identity,$ .quid ++);
I'm doing a fairly exhaustive series of DOM manipulations where a few elements (specifically form elements) have some events.
I am dynamically creating (actually cloning from a source element) several <select>
boxes and assigning a change() event to them.
The change event executes, and within the context of the event, "this" is the HTML Element Object.
What I need to do at this point however is determine a context for this HTML Element Object. I have these objects stored already as jQuery entities in assorted arrays, but obviously
[HTMLElement Object] != [Object Object]
And the trick is that I cannot cast $(this) and make a valid comparison since that would create a new object and the pointer would be different.
So... I've been banging my head against this for a while. In the past I've been able to circumvent this problem by doing an innerHTML comparison, but in this case the objects I am comparing are 100% identical, just there's lots of them. Therefore I need a solid comparison.
This would be easy if I could somehow derive the HTMLElement object from my originating jQuery object.
Thoughts, other ideas? Help. :(
Can't you just use $(this).data("something")
to keep data on your elements and then check the values later? (That's assuming you can't just give these things plain ol' "id" values.)
Oh also jQuery itself has a "guid" element that you can use (be careful!)
$(myNewObject).data("identity", $.quid++);
这篇关于从jQuery对象派生HTMLElement对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!