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
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.