search

Home  >  Q&A  >  body text

javascript - Newbie, please tell me why the jquery method can be used on this DOM object?

<!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~~~~

世界只因有你世界只因有你2731 days ago982

reply all(2)I'll reply

  • ringa_lee

    ringa_lee2017-07-05 11:01:54

    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~

    reply
    0
  • 淡淡烟草味

    淡淡烟草味2017-07-05 11:01:54

    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")

    reply
    0
  • Cancelreply