Javascript JQuery无法隐藏图像

Javascript JQuery无法隐藏图像,javascript,jquery,Javascript,Jquery,我在一个名为gallery的类中包装了一些图像。然后我试图在DocumentReady上隐藏这个类,但它不起作用。我做错了什么 <html> <head> <title>Galleria Jquery</title> <script src="jquery.min.js"></script> <script> <!-- Inserire in questa sez

我在一个名为gallery的类中包装了一些图像。然后我试图在DocumentReady上隐藏这个类,但它不起作用。我做错了什么

<html>

<head>
    <title>Galleria Jquery</title>
    <script src="jquery.min.js"></script>

    <script>
        <!-- Inserire in questa sezione il codice javascript -->

        $("img").wrapAll("<div class='gallery'></div>");
        $(document).ready(function(){
                          $(".gallery").hide();
                          });

    </script>

    <style>
        <!-- Inserire in questa sezione le definizione di stile -->

    </style>



</head>

<body>

    <img src="img1.jpg" />
    <img src="img2.jpg" />
    <img src="img3.jpg" />
    <img src="img4.jpg" />

    <button id="prevbutton">Prev</button>
    <button id="nextbutton">Next</button>

</body>
将js代码放入document.ready中

这是因为在执行js代码时,DOM中不存在imgtag

$(document).ready(function(){
 // your code
})
或者,您可以将js代码放在结束body标记附近

试试这个

<html>

<head>
    <title>Galleria Jquery</title>
    <script src="jquery.min.js"></script>


    <style>
        <!-- Inserire in questa sezione le definizione di stile -->

    </style>



</head>

<body>

    <img src="img1.jpg" />
    <img src="img2.jpg" />
    <img src="img3.jpg" />
    <img src="img4.jpg" />

    <button id="prevbutton">Prev</button>
    <button id="nextbutton">Next</button>
    <script>
        <!-- Inserire in questa sezione il codice javascript -->
        $("img").wrapAll("<div class='gallery'></div>");
        $(document).ready(function(){
            $(".gallery").hide();
        });

    </script>
</body>
</html>
试试这个

$(document).ready(function(){
  $("img").wrapAll("<div class='gallery'></div>");
  $(".gallery").hide();
});

因为wrappall是同步的,所以只有在将新的包装添加到DOM之后才会调用hide

谢谢!我能再问一个问题吗?我找不到我的第一个孩子。我已经尝试过$.gallery img:first-child.show上面的代码使用class.gallery从div数组中选择第一个div,然后选择其中的img并调用其中的show,我假设这是您需要的。
$(document).ready(function(){
  $("img").wrapAll("<div class='gallery'></div>");
  $(".gallery").hide();
});