代码之家  ›  专栏  ›  技术社区  ›  Craig McQueen

剥离jQuery中的链接

  •  5
  • Craig McQueen  · 技术社区  · 17 年前

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

    3 回复  |  直到 17 年前
        1
  •  8
  •   Shog9    17 年前
    $("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.
       });
    
        2
  •  4
  •   Andrew Moore    17 年前

    $('a[id^=a]').each(function() { $(this).replaceWith($(this).html()); });
    
        3
  •  1
  •   John Dibling    14 年前

    在纯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>