欢迎来到Doc100.Net免费学习资源知识分享平台!
您的位置:首页 > 程序异常 >

(三)选择元素——(14)接触dom元素(accessing dom elements)

更新时间: 2014-01-05 02:24:46 责任编辑: Author_N1

 

(3)选择元素——(14)接触DOM元素(Accessing DOM elements)
Every selector expression and most jQuery methods return a jQuery object. This is almost always what we want, because of the implicit iteration and chaining capabilities that it affords.

每一个选择器表达式和大部分的jquery方法返回了一个jquery对象。至几乎总是我们想要的,因为它提供了隐式迭代和链锁所用。

Still, there may be points in our code when we need to access a DOM elementdirectly. For example, we may need to make a resulting set of elements available to another JavaScript library. Additionally, we might need to access an element's tag name, which is available as a propertyof the DOM element. For these admittedly rare situations, jQuery provides the .get()method. To access the first DOM element referred to by a jQuery object, we would use .get(0). If the DOM element is needed within a loop, then we would use .get(index). So, if we want to know the tag name of an element with id="my-element", we would write the following code snippet:

var myTag = $('#my-element').get(0).tagName;

For even greater convenience, jQuery provides a shorthand for .get(). Instead of writing the preceding line, we can use square brackets immediately following the selector:

var myTag = $('#my-element')[0].tagName;

为了更好的便利性,jquery为get()提供了一个快捷方式。我们可以在选择器后面使用中括号,而不用写之前的代码:

var myTag = $('#my-element')[0].tagName;

It's no accident that this syntax appears to treat the jQuery object as an array of DOM elements; using the square brackets is like peeling away the jQuery wrapper to get at the node list, and including the index(in this case, 0) is like plucking out the DOM element itself.

毫无意外,这个语法看起来想是我们把jquery对象当成DOM元素数组来看待,使用方括号好像是去到了jquery对象的外包装,然后得到了dom元素列表,包括index(在这里是0),好像是把DOM元素自己扯了出来。

上一篇:上一篇
下一篇:下一篇

 

随机推荐程序问答结果

 

 

如对文章有任何疑问请提交到问题反馈,或者您对内容不满意,请您反馈给我们DOC100.NET论坛发贴求解。
DOC100.NET资源网,机器学习分类整理更新日期::2014-01-05 02:24:46
如需转载,请注明文章出处和来源网址:http://www.doc100.net/bugs/t/9472/
本文WWW.DOC100.NET DOC100.NET版权所有。