簡體   English   中英

在jQuery中刪除鏈接

[英]Stripping out a link in jQuery

我有一點像這樣的HTML:

<a href="#somthing" id="a1"><img src="something" /></a>
<a href="#somthing" id="a2"><img src="something" /></a>

我需要剝離鏈接,所以我只剩下幾個圖像標簽。 使用jQuery執行此操作的最有效方法是什么?

$("a > img").parent()   // match all <a><img></a>, select <a> parents
   .each( function()    // for each link
   { 
      $(this).replaceWith(              // replace the <a>
         $(this).children().remove() ); // with its detached children.
   });

這應該這樣做:

$('a[id^=a]').each(function() { $(this).replaceWith($(this).html()); });

在普通的JavaScript中,它將是這樣的:

<script type="text/javascript">
window.onload = function(){
  var l = document.getElementsByTagName("a");
  for(i=0, im=l.length; im>i; i++){
    if(l[i].firstChild.tagName == "img"){
      l[i].parentNode.replaceChild(l[i].firstChild,l[i]);
    }
  }
}
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM