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 对象的转换</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节点div转化为$div的jquery对象 var $first = $div.first(); //找到第一个div元素 $first.css('color', 'red'); //给第一个元素设置颜色 </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]; //转换成dom对象 div.style.color = "red";//对dom对象进行操作 </script> </body> </html>