[英]Remove wrapping <a> anchor tag from an image using jQuery
我試圖刪除圖像IF的第一個包裝標簽
<div class="feature">
<a>
<img width="252" height="79" alt="" src="http://localhost:81/site/wp-
content/uploads/2011/12/home-highlights.jpg" title="home-highlights"
class="alignnone size-full wp-image-55">
</a>
</div>
我已經看了很多選項,我認為我的方法是正確的:
$(".feature img").closest('a').remove();
如果我使用上面的例子,它也會刪除圖像,這當然不是我想要的。
jQuery有一個內置函數: unwrap
:
$(".feature a > img").unwrap();
unwrap
文檔 :
從DOM中刪除匹配元素集的父元素,將匹配的元素保留在原位。
child(>)
選擇器文檔 :
描述:選擇由“parent”指定的元素“child”指定的所有直接子元素。
謝謝@am不是我!
JSFiddle DEMO
解包方法是你想要的方法:
$(".feature a").children('img').unwrap();
你是對的,它將刪除元素和任何子元素。 試試這個,將圖像保存為變量並替換div的html:
var myImg = $('.feature img');
$('.feature').html(myImg);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.