如何更改<img/>标记的内部html?

时间:2016-08-29 08:00:11

标签: jquery html image attr

这是输出:

<img width="1080" height="1080" src="/image.jpg" class="post-image" alt="image" /> 

我需要将其更改为:(srcdata

<img width="1080" height="1080" data="/image.jpg" class="post-image" alt="image" /> 

2 个答案:

答案 0 :(得分:1)

您可以使用jQuery的attrremoveAttrgetsetremove属性。

&#13;
&#13;
var img = $("img");
img.attr("data", img.attr("src")).removeAttr("src");
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img width="1080" height="1080" src="/image.jpg" class="post-image" alt="image" />
&#13;
&#13;
&#13;

答案 1 :(得分:1)

只需使用下面简单的JQ:

首先获取src,然后将其添加为data,然后移除src

&#13;
&#13;
var src = $("img").attr("src")
$("img").attr("data", src).removeAttr("src")
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<img width="1080" height="1080" src="/image.jpg" class="post-image" alt="image" />
&#13;
&#13;
&#13;