使用jQuery将img标签转换为div标签 [英] img tag convert into div tag by using jQuery

查看:152
本文介绍了使用jQuery将img标签转换为div标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过使用jQuery将页面的所有img标签更改为div标签. 例如.

I want to change all img tag of a page into div tag by using jQuery. ex.

<img src="http://www.xyz.com/img.png" alt="this text i want in div" />

通过使用jQuery,它将转换为

by using jQuery it converts into

<div>this text i want in div</div>

我们如何使用jQuery做到这一点?

How we can do this by using jQuery?

推荐答案

您可以使用 replaceWith

这是一个小提琴: http://jsfiddle.net/Am75c/

<img src="http://www.xyz.com/img.png" alt="this text i want in div1" />
<img src="http://www.xyz.com/img.png" alt="this text i want in div2" />
<img src="http://www.xyz.com/img.png" alt="this text i want in div3" />

$('img').each(function(){
    $div = $('<div>').html($(this).attr('alt'));
    $(this).replaceWith($div);
});

这篇关于使用jQuery将img标签转换为div标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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