如何用div标签包装img元素 [英] How to wrap img element with a div tag

查看:185
本文介绍了如何用div标签包装img元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将使用div标签包装我的所有图像,该标签有一个名为image的类我就像 jquery网站但它不起作用。
这是我的代码:

i am going to wrap all my image with a div tag that has a class named image i'm doing just like the jquery website but it doesn't work. this is my code:

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

这是我的html代码:

and this is my html code:

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


推荐答案

你需要遍历图像并包装它们

You need to loop through the images and wrap them

$(".post img").each(function(index, element) {
    $(element).wrap("<div class='new'></div>");
});

演示

这篇关于如何用div标签包装img元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆