HTML5 practice and Analysis of CSS selector querySelector (1)

Source: Internet
Author: User

Today, we will introduce the HTML5 knowledge. We will introduce you to the topic of the new selector. The selector introduced today is querySelector (). I will share with you the comparison between jQuery and JavaScript. I hope this will help you learn HTML5.

The querySelector () method receives a CSS selector and returns the first element that matches the pattern. If no matching element is found, null is returned. An example is as follows:

 

1) Obtain tags

 

JavaScript code

 

var body = document.querySelector('body');        body.style.background = "red";        alert(body)//[object HTMLDivElement]  

 

 

JQuery code

  1. var $body = $("body");    $body.css({      'background': "red"  });    alert($body) //[object Object]  

 

Preview Effect

 

1. JavaScript

 

 

2. jQuery

 

 

 

2) Get the ID

 

HTML code

 
Menglong xiaozhan
Menglong xiaozhan

 

 

JavaScript code

Var box1 = document. querySelector ('# box1'); box1.innerHTML = "this is box1"; alert (box1) // [object HTMLDivElement]

 

 

JQuery code


 

Var $ box1 = $ ("# box1"); export box1.html ("this is implemented using jQuery"); alert ($ box1) // [object Object]

 

Preview Effect

 

1. JavaScript

 

 

2. jQuery

 

 

 

When the querySelector () method is called by the document type, the matching elements are searched within the scope of the document elements. When the querySelector () method is called by the element type, the matching element is only located within the range of the child element of the element. CSS separators can be simple and complex, depending on the situation. If an unsupported selector is input, querySelector () reports an error.

The CSS selector-querySelector () for HTML5 practice and analysis is introduced here. Considering that HTML5 has a relatively large amount of experience, there are many things in it, so the specific knowledge points will be described in one article, so each article may not be too long. I hope you can understand the benefits of doing so for future convenience. Thank you for your support and love. For more HTML5-related content, please pay attention to the update of Menglong xiaozhan's HTML5 practice and analysis.


Related Article

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.