Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何用div标记包装img元素_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何用div标记包装img元素

Javascript 如何用div标记包装img元素,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我将用一个div标记来包装我的所有图像,这个div标记有一个名为image的类,我正在做的工作与。 这是我的代码: <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $( ".post i

我将用一个div标记来包装我的所有图像,这个div标记有一个名为image的类,我正在做的工作与。 这是我的代码:

<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
    <script type="text/javascript">

$(document).ready(function(){

    $( ".post img" ).wrap( "<div class='new'></div>" );
});
    </script>

$(文档).ready(函数(){
$(“.post img”).wrap(“”);
});
这是我的html代码:

<div class="post">
        <img src="some where" alt="here"/>
        <img src="some where" alt="here"/>
        <img src="some where" alt="here"/>
        <img src="some where" alt="here"/>                                    
    </div>

我将用一个div标签包装我的所有图像

为了将所有图像包装在一个包装器中,您需要改用方法

$(".post img").wrapAll("<div class='new'></div>");
$(“.post img”).wrapAll(“”);


但是,要将每个图像包装到一个单独的div中,您需要为我编写代码。

您需要在图像中循环并包装它们

$(".post img").each(function(index, element) {
    $(element).wrap("<div class='new'></div>");
});
$(“.post img”)。每个(函数(索引,元素){
$(元素)。换行(“”);
});

对我来说很好->你是说把所有的图片都放在同一个DIV中,还是把每个图片放在一个单独的DIV中(这就是你现在正在做的)?那么你所拥有的应该可以很好地工作?