简体   繁体   English

JQuery方法和DOM属性

[英]JQuery methods and DOM properties

I am confused as to when I can use the DOM properties and when I could use the Jquery methods on a Jquery object. 我很困惑何时可以使用DOM属性以及何时可以在Jquery对象上使用Jquery方法。 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. 此时,$ elemSel是一个jquery对象,我理解它是DOM元素数组的包装器。 I could get a reference to the DOM elements by iterating through the $elemSel object/array (Correct?) 我可以通过迭代$ elemSel对象/数组来获取对DOM元素的引用(正确吗?)

My questions: 1. Is there a way to convert this $elemSel into a non JQuery regular array of DOM elements? 我的问题:1。有没有办法将这个$ elemSel转换为非JQuery常规DOM元素数组? 2. Can I combine DOM properties and JQuery methods at the same time (something like this) 2.我可以同时组合DOM属性和JQuery方法(类似这样)

$elemSel.children('td').nodeName

(nodeName is DOM related, children is JQuery related) (nodeName与DOM相关,children与JQuery相关)

EDIT: What's wrong with this? 编辑:这有什么问题?

$elemSel.get(0).is(':checked')

EDIT 2: 编辑2:

Thanks for the responses. 谢谢你的回复。 I understand now that I can use the get(0) to get a DOM element. 我现在明白我可以使用get(0)来获取DOM元素。 Additional questions: 其他问题:

  1. How would I convert a DOM element to a JQuery object? 我如何将DOM元素转换为JQuery对象?

  2. If I assign "this" to a variable, is that new var DOM or JQuery? 如果我将“this”赋给变量,是新的var DOM还是JQuery? If it's JQuery, how can I convert this to a DOM element? 如果是JQuery,我该如何将其转换为DOM元素? (Since I can't use get(0)) (因为我不能使用get(0))

    var $elemTd = $(this); var $ elemTd = $(this);

  3. When I do a assignment like the one above, I have seen some code samples not include the $ sign for the variable name. 当我执行上述任务时,我看到一些代码示例不包含变量名称的$符号。 Why? 为什么?

  4. And as for my original question, can I combine the DOM properties and JQuery functions at the same time on a JQuery object? 至于我最初的问题,我可以在JQuery对象上同时组合DOM属性和JQuery函数吗?

    $elemSel.children('td').nodeName $ elemSel.children( 'TD')。节点名称

You'll need to .get(0) the result to get the DOM-ready object. 你需要.get(0)结果来获得DOM-ready对象。

var myBox = $("div#myBox");
alert(myBox.get(0).id); // "myBox"

Read " Peeling Away the jQuery Wrapper and Finding an Array " by Cody Lindley 阅读Cody Lindley撰写的“ 剥离jQuery Wrapper和寻找数组


Re: Edit: .is() is not a native javascript method. Re:编辑: .is()不是原生的javascript方法。 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. 当你运行.get(0) ,你不再使用jQuery对象,因此你不能期望从它运行jQuery方法。

If you want to run .is() on a specific result, use the :eq(index) selector , or the .eq(index) method : 如果要对特定结果运行.is() ,请使用:eq(index) selector.eq(index) method

$("div:eq(1)").is(":checked"); // gets second div
$("div").eq(1).is(":checked"); // gets second div

Re: Edit # 2 回复:编辑#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: 将dom元素转换为jquery对象是通过在选择器中传递它来完成的:

var myBox = document.createElement("div");
var myBoxJQ = $(myBox);

Assinging This to a variable. This分配给变量。 Depends on when you do it. 取决于你什么时候做。 If by "this" you're referring to a jQuery object, then this will be a jQuery object. 如果通过“this”你指的是jQuery对象,那么this将是一个jQuery对象。 You can convert it by following this with .get(0) . 您可以通过以下步骤转换它this.get(0)

When this is referring to a jQuery object, you don't need to wrap it in the $(). this是指jQuery对象时,您不需要将其包装在$()中。 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. 最后, $elemSel.children('td').nodeName可以像这样完成: $elemSel.children('td')[0].nodeName$elemSel.children('td').get(0).nodeName ,其中0是要访问的项的索引。

There is also a shortcut for get(index) function: get(index)函数还有一个快捷方式:

$(selector)[0].nodeName

Soruce: http://docs.jquery.com/Core/get#index Soruce: http//docs.jquery.com/Core/get#index

Because there can be more than one match in jQuery selectors you need to get the elements out of the "array". 因为jQuery选择器中可以有多个匹配项,所以需要从“数组”中获取元素。

You can use the get method to return a single DOM element or an array or DOM elements. 您可以使用get方法返回单个DOM元素或数组或DOM元素。

eg: 例如:

var elims = $("div").get();
for(var i in elims)
    alert(elims[i].nodeName);

Edit: 编辑:

$elemSel.get(0).is(':checked') will not work because you are getting the Dom object and then trying to use jQuery functions on it. $elemSel.get(0).is(':checked')将无效,因为您正在获取Dom对象,然后尝试在其上使用jQuery函数。 If you want to get a single jQuery element use eq . 如果你想获得一个jQuery元素,请使用eq

$elemSel.eq(0).is(':checked');

In order to call native DOM method you can use get(index). 为了调用本机DOM方法,您可以使用get(index)。 Or if you want to use jQuery Object method, you should wrap a native DOM element with jQuery, which will convert the DOM element into a jQuery Object, so all the methods will be available. 或者如果你想使用jQuery Object方法,你应该用jQuery包装一个原生DOM元素,它将把DOM元素转换成一个jQuery对象,所以所有方法都可用。 http://www.linxinshan.com/?p=339 http://www.linxinshan.com/?p=339

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM