【问题标题】:Replace the attribute rel with href using jQuery - preserve the value使用 jQuery 将属性 rel 替换为 href - 保留值
【发布时间】:2010-11-21 00:46:37
【问题描述】:

我有更多的链接,例如:

<a rel="custom_link1_to_large_image">thumb</a>

有没有办法保留链接并改为使用href: href="custom_link1_to_large_image"

我需要让它与灯箱一起工作,但我无法手动添加它,因为它是由 NextGen 从 WP 自动生成的。

谢谢!

【问题讨论】:

    标签: jquery attributes href


    【解决方案1】:
    $("a").each(function(){
        $(this).attr("href", $(this).attr("rel"));
    });
    

    以上将使所有链接来自:

    <a rel="custom_links">..</a>
    

    成为

    <a rel="custom_links" href="custom_links">...</a>
    

    【讨论】:

      【解决方案2】:

      应该这样做:

      $('a[rel]').attr('href', function() {
          return $('this').attr('rel');
      }).attr('rel', '');
      

      【讨论】:

        【解决方案3】:

        我会这样做:

        $('a[rel]').attr('rel',function(i,rel){ this.href = rel; return null; });
        

        示例: http://jsfiddle.net/patrick_dw/xnyr5/

        如果您想保留rel,只需删除return null;

        【讨论】:

        • 谢谢,这看起来有点复杂,但确实有效。查看其他答案,这些也应该有效。
        • @Lucian - 不客气。它可能看起来很复杂,因为它在一条线上。用换行符分解函数的主体,它可能看起来更干净一些。如果您不想摆脱rel 属性,我会更改它to this$('a[rel]').attr('href',function(){return this.rel;});
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多