javascript - Newbie, please tell me why the jquery method can be used on this DOM object?
世界只因有你
世界只因有你 2017-07-05 10:59:54
0
2
932
<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="http://code.jquery.com/jquery-1.11.3.js"></script>
</head>
 
<body>
    <p>元素一</p>
    <p>元素二</p>
    <p>元素三</p>
     
    <script type="text/javascript">
        $p = $(document.getElementsByTagName("p"));
        $p.css("color","red");
    </script>
     
</body>
 
</html>

$p = $(document.getElementsByTagName("p"));Isn't this a dom object? Why is it still useful if I use the jquery method below? I tested that if the above is a jquery object and the dom operation method is replaced below, it will not work. . . Also, why do jquery objects and dom objects need to be converted to each other? Under what circumstances conversion is required. Please ask God to explain this to Xiaobai. Thank you very much~~~~

世界只因有你
世界只因有你

reply all(2)
ringa_lee

jquery little white children's shoes, http://www.css88.com/jqapi-1.... It is recommended to read more jquery documents, it is quite clear, $()If the parameter is dom, then encapsulate it directly It becomes a jquery object. $p is a jQuery object. Naturally, jquery methods can be used. However, jquery objects are not equivalent to dom. You cannot directly use the native operations of dom. Maybe you can get the dom object through $p[0]~ Suggestions Look through the document~

淡淡烟草味

It must be a jquery object, because $(documnet) converts the document into a jQuery object. You can take a look at the official documentation.
Your writing is equivalent to $("p")

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template