代码之家  ›  专栏  ›  技术社区  ›  meleyal

如何使用jQuery使链接标题与其URL相同?

  •  1
  • meleyal  · 技术社区  · 16 年前

    我的目标是使每个外部链接的标题与其href属性相等。

    title attr 作用

    $('a').filter(function() {
    
        var title= $(this).attr('href');
        return this.hostname && this.hostname !== location.hostname;
    
     })
        .removeAttr('target')
        .attr('rel', 'external')
        .attr('title', title);
    
    $('a[rel="external"]').click( function() {
        window.open( $(this).attr('href') );
        return false;
    });
    

    我想我有点倒转了,答案是 attr(key, fn)

    1 回复  |  直到 16 年前
        1
  •  3
  •   Paolo Bergantino    16 年前
    $('a').each(function() {
        var href = $(this).attr('href');
        if(this.hostname && this.hostname !== location.hostname) {
            $(this).removeAttr('target')
                   .attr('rel', 'external')
                   .attr('title', href)
                   .click(function() {
                       window.open(href);
                       return false;
                   });
        } 
    });