使用 JQuery 从 html 和字符串内容中获取图像标签。我想需要图像标签表单内容并将其替换为它的 alt 属性。为了。例子:
鉴于我的 html 字符串内容。
Hello this is long content with multiple images
<img alt=":*" src="kissing_heart.png"> and smiley is <img alt=":D" src="smiley.png">.
那怎么办呢?
请检查我已使用您的代码更新的此输出。这就是你想要的,只有我做的事情,我将所有内容包装在一个 div 中并迭代每个图像以替换它的 alt 属性。
$("#mycontent img").each(function () {
var altText = $(this).attr("alt");
$(this).replaceWith(altText);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div id="mycontent">
Hello this is long content with multiple images
<img alt=":*" src="kissing_heart.png"> and smiley is <img alt=":D" src="smiley.png">.
</div>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句