简体   繁体   English

jQuery对象和DOM元素

[英]jQuery object and DOM element

I would like to understand relationship between jQuery object and DOM element.. 我想了解jQuery对象和DOM元素之间的关系..

When jQuery returns an element it shows up as [object Object] in an alert. 当jQuery返回一个元素时,它在警报中显示为[object Object] When getElementByID returns an element it shows up as [object HTMLDivElement] . getElementByID返回一个元素时,它显示为[object HTMLDivElement] What does that mean exactly? 这究竟是什么意思? I mean are both of them objects with a difference ? 我的意思是他们俩都是有区别的对象吗?

Also what methods can operate on jQuery object vs DOM element? 还有什么方法可以对jQuery对象和DOM元素进行操作? Can a single jQuery object represent multiple DOM elements ? 单个jQuery对象可以表示多个DOM元素吗?

I would like to understand relationship between jQuery object and DOM element 我想了解jQuery对象和DOM元素之间的关系

A jQuery object is an array-like object that contains DOM element(s). jQuery对象是一个包含DOM元素的类数组对象。 A jQuery object can contain multiple DOM elements depending on the selector you use. jQuery对象可以包含多个DOM元素,具体取决于您使用的选择器。

Also what methods can operate on jQuery object vs DOM element? 还有什么方法可以对jQuery对象和DOM元素进行操作? Can a single jQuery object represent multiple DOM elements ? 单个jQuery对象可以表示多个DOM元素吗?

jQuery functions (a full list is on the website) operate on jQuery objects and not on DOM elements. jQuery函数(网站上的完整列表)对jQuery对象进行操作,而不是对DOM元素进行操作。 You can access the DOM elements inside a jQuery function using .get() or accessing the element at the desired index directly: 您可以使用.get()访问jQuery函数内的DOM元素,或者直接访问所需索引处的元素:

$("selector")[0] // Accesses the first DOM element in this jQuery object
$("selector").get(0) // Equivalent to the code above
$("selector").get() // Retrieve a true array of DOM elements matched by this selector

In other words, the following should get you the same result: 换句话说,以下内容应该会得到相同的结果:

<div id="foo"></div>

alert($("#foo")[0]);
alert($("#foo").get(0));
alert(document.getElementById("foo"));

For more information on the jQuery object, see the documentation . 有关jQuery对象的更多信息, 请参阅文档 Also check out the documentation for .get() 另请查看.get()的文档

When you use jQuery to obtain an DOM element, the jQuery object returns contains a reference to the element. 当您使用jQuery获取DOM元素时,jQuery对象返回包含对该元素的引用。 When you use a native function like getElementById , you get the reference to the element directly, not contained within a jQuery object. 当您使用getElementById等本机函数时,您将直接获得对元素的引用,而不是包含在jQuery对象中。

A jQuery object is an array-like object that can contain multiple DOM elements: jQuery对象是一个类似于数组的对象,可以包含多个DOM元素:

var jQueryCollection = $("div"); //Contains all div elements in DOM

The above line could be performed without jQuery: 上面的行可以在没有jQuery的情况下执行:

var normalCollection = document.getElementsByTagName("div");

In fact, that's exactly what jQuery will do internally when you pass in a simple selector like div . 事实上,当你传入像div这样的简单选择器时,这正是jQuery内部所做的。 You can access the actual elements within a jQuery collection using the get method: 您可以使用get方法访问jQuery集合中的实际元素:

var div1 = jQueryCollection.get(0); //Gets the first element in the collection

When you have an element, or set of elements, inside a jQuery object, you can use any of the methods available in the jQuery API, whereas when you have the raw element you can only use native JavaScript methods. 当你在jQuery对象中有一个元素或一组元素时,你可以使用jQuery API中的任何可用方法,而当你有raw元素时,你只能使用本机JavaScript方法。

I just barely started playing with jQuery this last month, and I had a similar question running around in my mind. 我上个月刚刚开始玩jQuery,我心里也有类似的问题。 All the answers you have received so far are valid and on the dot, but a very precise answer may be this: 到目前为止,您收到的所有答案都是有效的,但有一个非常精确的答案可能是:

Let's say you are in a function, and to refer to the calling element, you can either use this , or $(this) ; 假设你在函数中,并且要引用调用元素,你可以使用this ,或者$(this) ; but what is the difference? 但有什么区别? Turns out, when you use $(this) , you are wrapping this inside a jQuery object. 原来,当你使用$(this) ,你包装this一个jQuery对象中。 The benefit is that once an object is a jQuery object, you can use all the jQuery functions on it. 好处是,一旦一个对象是一个jQuery对象,你就可以使用它上面的所有jQuery函数。

It's pretty powerful, since you can even wrap a string representation of elements, var s = '<div>hello <a href='#'>world</a></div><span>!</span>' , inside a jQuery object just by literally wrapping it in $(): $(s) . 它非常强大,因为你甚至可以包装元素的字符串表示, var s = '<div>hello <a href='#'>world</a></div><span>!</span>' ,在jQuery对象中,只需将其包装在$(): $(s) Now you can manipulate all those elements with jQuery. 现在,您可以使用jQuery操作所有这些元素。

Most jQuery member Functions do not have a return value but rather return the current jQuery Object or another jQuery Object . 大多数jQuery成员Functions没有返回值,而是返回当前的jQuery Object或另一个jQuery Object


So, 所以,

console.log("(!!) jquery >> " + $("#id") ) ; 

will return [object Object] , ie a jQuery Object which maintains the collection which is the result of evaluating the selector String ( "#id" ) against the Document , 将返回[object Object] ,即一个jQuery Object ,它维护集合,该集合是对Document评估选择器String"#id" )的结果,

while , 而,

console.log("(!!) getElementById >> " + document.getElementById("id") ) ;

will return [object HTMLDivElement] (or in fact [object Object] in IE) because/if the return value is a div Element . 将返回[object HTMLDivElement] (或实际上是IE中的[object Object] ),因为/如果返回值是div Element


Also what methods can operate on jQuery object vs DOM element? 还有什么方法可以对jQuery对象和DOM元素进行操作? (1) Can a single jQuery object represent multiple DOM elements ? (1)单个jQuery对象可以表示多个DOM元素吗? (2) (2)

(1) There is a host of member Function s in jQuery that pertain to DOM Object s. (1)jQuery中有许多与DOM Object相关的成员Function The best thing to do imo is search the jQuery API documentation for a relevant Function once you have a specific task (such as selecting Node s or manipulating them). 做imo的最好的事情是,一旦你有一个特定的任务(比如选择Node或操纵它们),就可以在jQuery API文档中搜索相关的Function

(2) Yes, a single jQuery Object may maintain a list of multiple DOM Element s. (2)是的,单个jQuery Object可以维护多个DOM Element的列表。 There are multiple Functions (such as jQuery.find or jQuery.each ) that build upon this automatic caching behaviour. 有多个Functions (例如jQuery.findjQuery.each )构建于此自动缓存行为之上。

That's just your browser being clever. 那只是你的浏览器很聪明。 They're both objects but DOMElements are special objects. 它们都是对象,但是DOMElements是特殊对象。 jQuery just wraps DOMElements in a Javascript object. jQuery只是将DOMElements包装在Javascript对象中。

If you want to get more debug info I recommend you look at debugging tools like Firebug for Firefox and Chrome's built-in inspector (very similar to Firebug). 如果您想获得更多调试信息,我建议您查看调试工具,如Firebug for Firefox和Chrome的内置检查器(非常类似于Firebug)。

Besides what has been mentioned, I'd like to add something about why jQuery object is imported according to description from jquery-object 除了已经提到的内容之外,我还想根据jquery-object中的描述添加一些关于为什么导入jQuery对象的东西

  • Compatibility 兼容性

The implementation of element methods varies across browser vendors and versions. 元素方法的实现因浏览器供应商和版本而异。

As an example, set innerHTML of element may not work in most versions of Internet Explorer. 例如,设置innerHTML元素可能无法在大多数版本的Internet Explorer中使用。

You can set innerHTML in jQuery way and jQuery will help you hide the differences of browser. 您可以使用jQuery方式设置innerHTML,jQuery将帮助您隐藏浏览器的差异。

// Setting the inner HTML with jQuery.

var target = document.getElementById( "target" );

$( target ).html( "<td>Hello <b>World</b>!</td>" );
  • Convenience 方便

jQuery provides a list of methods bound to jQuery object to smooth developer's experience, please check some of them under http://api.jquery.com/ . jQuery提供了绑定到jQuery对象的方法列表,以平滑开发人员的体验,请在http://api.jquery.com/下查看其中一些方法。 The website also provides a common DOM manipulation, let's see how to insert an element stored in newElement after the target element in both way. 该网站还提供了一个常见的DOM操作,让我们看看如何以两种方式在target元素之后插入存储在newElement的元素。

The DOM way, DOM的方式,

// Inserting a new element after another with the native DOM API.

var target = document.getElementById( "target" );

var newElement = document.createElement( "div" );

target.parentNode.insertBefore( newElement, target.nextSibling );

The jQuery way, jQuery的方式,

// Inserting a new element after another with jQuery.

var target = document.getElementById( "target" );

var newElement = document.createElement( "div" );

$( target ).after( newElement );

Hope this is a supplement. 希望这是一个补充。

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

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