Differences and conversions between JavaScript objects, DOM objects, and jquery objects
Source: Internet
Author: User
1. JavaScript provides multiple built-in objects, such as String, Date, and Array. Objects are only special data types with properties and methods. Access Object Attributes: [javascript] varmessage & quot; HelloWorld! & Quot; varxmessagele 1. JavaScript provides multiple built-in objects, such as String, Date, and Array. Objects are only special data types with properties and methods. Access Object Attributes: [javascript] var message = "Hello World! "; Var x = message. length; create a JavaScript Object: [javascript] person = new Object (); person. firstname = "Bill"; person. lastname = "Gates"; person. age = 56; person. eyecolor = "blue"; Use the object constructor: [javascript] view plaincopyfunction person (firstname, lastname, age, eyecolor) {this. firstname = firstname; this. lastname = lastname; this. age = age; this. eyecolor = eyecolor;} [javascript] var myFather = new person ("Bill", "Gates", 56, "blue "); Var myMother = new person (" Steve "," Jobs ", 48," green "); 2. DOM object DOM is actually a document model described in object-oriented mode. DOM defines the objects required to represent and modify documents, the behavior and attributes of these objects, and the relationship between these objects. According to W3C DOM specifications, DOM is an application programming interface (API) for HTML and XML ). With DOM, you can access all HTML elements, along with the text and attributes they contain. You can modify and delete the content and create new elements. Html dom is independent of platform and programming language. It can be used by any programming languages such as Java, JavaScript, and VBScript. DOM objects are the objects we obtain using traditional methods (javascript. Iii. jQuery object jquery object is actually a javascript array. This array object contains 125 methods and 4 properties. The four attributes are the current jquery Framework Version Number of jquery. length indicates the array object. the number of elements context generally points to the selector content passed by the HtmlDocument object selector, for example: # yourId or. yourClass, etc. If you get the jquery object through the $ ("# yourId") method, and your page has only one element whose id is yourId, then $ ("# yourId ") [0] is the HTML element and document. the elements obtained by getElementById ("yourId") are the same simple understanding, that is, the jQuery object created by jQuery is the object generated after the DOM object is packaged by jQuery. JQuery objects are unique to jQuery. They can use methods in jQuery, but they cannot use DOM methods. 4. Differences between DOM objects and jquery objects [javascript] var domObj = document. getElementById ("id"); // DOM object var $ obj =$ ("# id"); // jQuery object; [javascript] (("#img”).attr("“src”, ”test.jpg "); // here $ ("# img") is used to obtain the jQuery object; [javascript] document.getelementbyid(%img%%%%.src%%test.jpg "; // The document here. getElementById ("img") is the DOM object. Another example is this. I often write this.attr(javassrc#,”test.jpg when writing jQuery" ); But an error occurs. This is a domobject, and .attr(“src”, ”test.jpg ") is a jQuery method, so an error occurs. To solve this problem, we need to convert the DOM object to the jQuery object, for example, convert (this).attr(“src”, ”test.jpg "); 5. Convert the DOM object and the jquery object to the jQuery object: for a DOM object, you only need to wrap the DOM object with $ () to obtain a jQuery object. Method: $ (DOM object) [javascript] var v = document. getElementById ("v"); // DOM object var $ v =$ (v); // convert jQuery object to DOM object: two conversion methods are used to convert a jQuery object to a DOM object: [index] and. get (index); (1) the jQuery object is a data object. You can use the [index] method to obtain the corresponding DOM object. [Javascript] var $ v = $ ("# v"); // jQuery object var v = $ v [0]; // DOM object alert (v. checked) // check whether the checkbox is selected (2) provided by jQuery itself, through. get (index) method to get the corresponding DOM object. [Javascript] var $ v = $ ("# v"); // jQuery object var v = $ v. get (0); // DOM object alert (v. checked) // check whether the checkbox is selected
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service