The difference between $ ("# air") and document. getelementbyid ("Air") in jquery

Source: Internet
Author: User

I have never noticed that $ ("# air") in jquery is the same thing as document. getelementbyid ("Air"). It refers to the same thing. Write a canvasProgramThe two are different.

Directly use alert () to show what the two methods actually obtain.CodeAs follows:

 <!  Doctype html  >  
< Html >
< Head >
< Meta Charset = "UTF-8" >
< Title > Air </ Title >
< Link Href = "CSS/index.css" REL = "Stylesheet" Type = "Text/CSS" >
< Script SRC = "JS/jquery-1.4.2.min.js" Type = "Text/JavaScript" > </ Script >
</ Head >
< Body >
< Div Class = "Warp" >
< Canvas ID = "Air" > </ Canvas >
</ Div >
< Script >
VaR Canvas_air = $ ( " # Air " );
Alert (canvas_air );
Alert (document. getelementbyid ("Air "));
VaR Air_2d = Canvas_air.getcontext ( " 2d " );
VaR Air_img = New Image ();
Air_img.src = " Images/Boston-III-48px.png " ;
Air_2d.drawimage (air_img, 0 , 0 );
</ Script >
</ Body >
</ Html >

The Alert () values are [object] and [object htmlcanvaselement]. From here, it is not hard to see that $ ("# air") is not as expected. Let's take a look at firebug debugging,
$ ("# Air") and document. getelementbyid ("Air. The debugging result is as follows:

 
$ ("# Air") [Canvas # air]

Document. getelementbyid ("Air") Canvas # air

You may think of the results as well.

In fact, $ ("# air") [0] is equivalent to document. getelementbyid ("Air ")

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.