jquery uses the native JS method to Js/jquery objects to each other

Source: Internet
Author: User

JavaScript native There are many ways, jquery itself has a set of packaged methods, jquery's own selector is very convenient, but sometimes we need to use some native methods, if we use jquery to select elements, The method of calling native JavaScript directly will cause an error. For example, we want to output the width of a picture:

Native JS:

Javascript:
var dom1 = document.getElementById ("Pic"). Width;
Alert (DOM1)

Jquery:

var dom2 = $ ("#pic"). width ();
Alert (DOM5)

Html:

Both methods can output the width of the image, but what if we want to use the jquery selector $ ("#pic") and use the native width method? If direct $ ("#pic"). Width is an error. The reason is that $ ("#pic") gets the jquery object, and the native width method is the DOM attribute, so you need to convert it:

$ ("#pic") [0].width or $ ("#pic"). Get[0].width

jquery Object Goto JS Object
$ (' #id ') [0]
JS object to jquery Object
var obj = document.getElementById (' id ');
$ (obj)

jquery uses the native JS method to Js/jquery objects to each other

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.