JQuery方法和DOM属性 [英] JQuery methods and DOM properties
问题描述
var $ elemSel = $('#myDiv')。find('[id * = \\ \\'select \''')
此时,$ elemSel是一个jquery对象,理解为围绕数组DOM元素的包装。我可以通过遍历$ elemSel对象/数组(Correct?)来获取对DOM元素的引用。
我的问题:
1.有没有办法将此$ elemSel转换为非JQuery常量的DOM元素数组?
2.我可以同时结合DOM属性和JQuery方法(这样)
$ elemSel。子节点('td')nodeName
(nodeName是DOM相关的, p>
编辑:这是什么问题?
$ elemSel.get(0 ).is(':checked')
编辑2: p>
感谢您的回复。我现在明白,我可以使用get(0)获取一个DOM元素。其他问题:
-
如何将DOM元素转换为JQuery对象?
-
如果我将this分配给一个变量,那是新的var DOM还是JQuery?如果是JQuery,我该如何将其转换为DOM元素? (因为我不能使用get(0))
var $ elemTd = $(this);
-
当我做一个上述的作业时,我看到一些代码示例不包含变量名的$符号。为什么?
-
对于我原来的问题,我可以在一个JQuery对象上同时结合DOM属性和JQuery函数吗?
$ elemSel.children('td')。nodeName
您需要.get(0)结果才能获得DOM就绪对象。
var myBox = $(div#myBox);
alert(myBox.get(0).id); //myBox
阅读剥离jQuery包装器和查找数组by Cody Lindley
Re:编辑: .is()
不是本机的JavaScript方法。当您运行 .get(0)
时,您将不再使用jQuery对象,因此您无法期望从中运行jQuery方法。
如果要针对特定结果运行 .is()
,请使用 :eq(index)selector
,或 .eq(index)方法
:
$(div:eq(1))。is(:checked); //获得第二个div
$(div)。eq(1).is(:checked); //获得第二个div
Re:编辑#2
Bob,你真的应该创建新的
问题,而不是要求更多和
更多在这里。
将dom元素转换为jquery对象是通过将其传递给选择器来完成的:
var myBox = document.createElement(div);
var myBoxJQ = $(myBox);
将这个
赋给一个变量。取决于你做什么如果通过this指的是一个jQuery对象,那么这个
将是一个jQuery对象。您可以按照 与
.get(0)
。
当这个
指的是一个jQuery对象,你不需要将它包装在$()中。这是多余的。
最后, $ elemSel.children('td')。nodeName
可以像this: $ elemSel.children('td')[0] .nodeName
或
$ elemSel.children('td')。 get(0).nodeName
,其中0是要访问的项目的索引。
I am confused as to when I can use the DOM properties and when I could use the Jquery methods on a Jquery object. Say, I use a selector
var $elemSel = $('#myDiv').find('[id *= \'select\']')
At this point, $elemSel is a jquery object which I understand to be a wrapper around the array of DOM elements. I could get a reference to the DOM elements by iterating through the $elemSel object/array (Correct?)
My questions: 1. Is there a way to convert this $elemSel into a non JQuery regular array of DOM elements? 2. Can I combine DOM properties and JQuery methods at the same time (something like this)
$elemSel.children('td').nodeName
(nodeName is DOM related, children is JQuery related)
EDIT: What's wrong with this?
$elemSel.get(0).is(':checked')
EDIT 2:
Thanks for the responses. I understand now that I can use the get(0) to get a DOM element. Additional questions:
How would I convert a DOM element to a JQuery object?
If I assign "this" to a variable, is that new var DOM or JQuery? If it's JQuery, how can I convert this to a DOM element? (Since I can't use get(0))
var $elemTd = $(this);
When I do a assignment like the one above, I have seen some code samples not include the $ sign for the variable name. Why?
And as for my original question, can I combine the DOM properties and JQuery functions at the same time on a JQuery object?
$elemSel.children('td').nodeName
You'll need to .get(0) the result to get the DOM-ready object.
var myBox = $("div#myBox");
alert(myBox.get(0).id); // "myBox"
Read "Peeling Away the jQuery Wrapper and Finding an Array" by Cody Lindley
Re: Edit: .is()
is not a native javascript method. When you run .get(0)
, you are no longer working off of the jQuery object, therefore you cannot expect to run jQuery methods from it.
If you want to run .is()
on a specific result, use the :eq(index) selector
, or the .eq(index) method
:
$("div:eq(1)").is(":checked"); // gets second div
$("div").eq(1).is(":checked"); // gets second div
Re: Edit # 2
Bob, you really should create new questions, rather than asking more and more here.
Converting a dom element to jquery object is done by passing it in a selector:
var myBox = document.createElement("div");
var myBoxJQ = $(myBox);
Assinging This
to a variable. Depends on when you do it. If by "this" you're referring to a jQuery object, then this
will be a jQuery object. You can convert it by following this
with .get(0)
.
When this
is referring to a jQuery object, you don't need to wrap it in the $(). This is redundant.
And lastly, $elemSel.children('td').nodeName
can be done like this: $elemSel.children('td')[0].nodeName
or
$elemSel.children('td').get(0).nodeName
, where the 0 is the index of which item to access.
这篇关于JQuery方法和DOM属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!