Conversion between jQuery objects and DOM objects
jQuery is converted into DOM. In development, it is more common to process a DOM object into a jQuery object. $(parameter) is a multifunctional method that produces different effects by passing different parameters.
After processing the ordinary dom object into a jQuery object through the $(dom) method, we can call the jQuery method
Look at the following code:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>jQuery 對象 與DOM 對象的轉(zhuǎn)換</title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> </head> <body> <div>元素一</div> <div>元素二</div> <div>元素三</div> <script type="text/javascript"> var div = document.getElementsByTagName('div'); //這是一個DOM 對象 var $div = $(div); //jQuery 對象 將dom節(jié)點div轉(zhuǎn)化為$div的jquery對象 var $first = $div.first(); //找到第一個div元素 $first.css('color', 'red'); //給第一個元素設(shè)置顏色 </script> </body> </html>
How to Convert jQuery object into DOM object
The jQuery library is essentially JavaScript code. It just wraps the JavaScript language in order to provide better, more convenient and faster DOM processing and functions commonly used in development. When we use jQuery, we can also mix it with JavaScript native code. In many scenarios, we need jQuery and DOM to be able to convert each other. They are both operable DOM elements. jQuery is an array-like object, and the DOM object is a single DOM element.
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script> </head> <body> <div>元素一</div> <div>元素二</div> <div>元素三</div> <script type="text/javascript"> var $div = $('div'); //jquery對象 var div = $div[1]; //轉(zhuǎn)換成dom對象 div.style.color = "red";//對dom對象進行操作 </script> </body> </html>