javascript - 使用 jQuery 从图像中删除包装 <a> anchor 标记

标签 javascript jquery html css anchor

我正在尝试删除图像的第一个包装标签如果存在

   <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();

展开 docs :

Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.

child(>) 选择器 docs :

Description: Selects all direct child elements specified by "child" of elements specified by "parent".

谢谢@am 不是我!
<强> JSFiddle DEMO

关于javascript - 使用 jQuery 从图像中删除包装 <a> anchor 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9295292/

相关文章:

javascript - 动态添加的文本id,getElementById

javascript - 主干网看不到传递给模板的 key

javascript - Node.js 服务器目录结构应该是什么样的?

javascript - 如何在 jQuery.each() util 中跳到下一次迭代?

javascript - 使用 JQuery Range Slider 实时更新 CSS 值

javascript - 从 Gnome-Shell 扩展中使用 C 库

javascript - 如何为给定对象附加标签列表

css - <th> 文本对齐兼容性

javascript - 确定下拉菜单的选定索引号?

javascript - 加载脚本时修改 div 高度