最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Stripping out a link in jQuery - Stack Overflow

programmeradmin4浏览0评论

I have a bit of html like so:

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

I need to strip off the links so I'm just left with a couple of image tags. What would be the most efficient way to do this with jQuery?

I have a bit of html like so:

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

I need to strip off the links so I'm just left with a couple of image tags. What would be the most efficient way to do this with jQuery?

Share Improve this question edited Oct 8, 2008 at 23:07 Shog9 160k36 gold badges235 silver badges240 bronze badges asked Oct 8, 2008 at 22:08 defrexdefrex 16.5k8 gold badges36 silver badges45 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 9
$("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.
   });

This should do it:

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

In plain javascript it would be something like:

<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>
发布评论

评论列表(0)

  1. 暂无评论