开发者

Removing duplicate anchor tags

I have a page with a lot of hrefs listed in table rows. The user can hide/show table rows depending on seletions. I'd like to temporarily change the hrefs for the duplicates into plain text.

My thought is to do a

$('a').each(function() {
});

And then within each a element, using the nextUntil method to change any links into text that equal this.href. But then what do I do when the user chooses to hide/show a different selection? How do I refresh the links and hide the duplicates according the new filter?

I suppose I could just refresh the page, but that wouldn't be as开发者_C百科 sexy.


I don't fully understand your question, but see if this does what you need:

( function( global )
{
  var $ = global.jQuery,
      uniqueHREFs = {};

  $( function()
  {
    var $matrixTable = $( '#matrix' ),
        $matrixTableChildren = $matrixTable.children().detach();

    $matrixTableChildren.find( 'a' ).each( function()
    {
      var $this = $( this ),
          HREF = $this.attr( 'href' );

      if( typeof uniqueHREFs[HREF] !== 'undefined' )
      {
        $this.replaceWith(
          $( '<span>' ).addClass( 'duplicate-link-replacement' ).html( $this.html() )
        );
      }
      else
      {
        uniqueHREFs[HREF] = true;
      }
    } );

    $matrixTable.append( $matrixTableChildren );
  } );
}( window ) );

It iterates all the links in your table and stores each HREF it hits which it has not previously seen. When it hits one it has previously seen, it replaces it with a span that has a class of duplicate-link-replacement and the same HTML which the link previously had.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜