【问题标题】:Jquery replace href with name [duplicate]Jquery用名称替换href [重复]
【发布时间】:2013-07-08 21:49:56
【问题描述】:

我有 26 行分散在单个页面的正文中,我需要在文件的 HEAD 中通过 jquery 对其进行更改。

<a href="A">A</a>
<a href="B">B</a>
<a href="C">C</a>
<a href="D">D</a>
<a href="E">E</a>
<a href="F">F</a>

等等

我需要制作所有 26 个实例的锚链接。所以我需要他们从'href'变成'name'。我无权直接更改正文内容,因此使用 jquery。

http://jsfiddle.net/deekster/ufRxr/5/

(出于演示目的,我手动更正了第一项 - A。将“结果”窗口缩小以查看 A 锚点的工作)。我感谢任何人的回复。

【问题讨论】:

  • 你尝试过什么(代码)来解决问题?
  • $("a").each(function() { $(this).attr("name", $(this).attr("href").split("#")[1]); $(this).removeAttr("href"); });
  • 可能是phpjs.org/functions/range 和使用 $(selector).html() 的菜单和页面导航的组合?

标签: jquery replace


【解决方案1】:

像这样?

$('a[href]').attr('name', function () {
    var href = this.href;
    return href.substr(href.lastIndexOf('#') + 1);
}).removeAttr('href');

Fiddle

或者

$('a[href]').attr('name', function () {
    var href = this.href;
    this.removeAttribute('href');
    return href.substr(href.lastIndexOf('#') + 1);
});

Fiddle

只是另一个正则表达式示例:

$('ul a[href]').attr('name', function () {
    return this.href.match(/#([^#]*)$/)[1];
}).removeAttr('href');

Fiddle

【讨论】:

  • 这会生成name 值,例如http://fiddle.jshell.net/eAkU9/show/A(例如)。 this.href 返回完整的 URL,而不是 href 属性的值。
  • @FelixKling 没错,。修好了。
  • 您好@PSL 谢谢回复。我已将您的代码粘贴到下面的 jsfiddle 中,但是它不起作用:jsfiddle.net/ufRxr/9
  • @user2291964 你没有看到更新的小提琴和答案,检查一下。
  • 你为什么不直接做return this.href.substr(this.href.lastIndexOf('#') + 1);?我高度怀疑访问this.href 两次会比访问href 两次更糟糕...
【解决方案2】:

这里是一个例子:

// select all a element with an href attribute after a ul element
$('ul ~ a[href]').each(function(){
    // set the href attribute to name
   $(this).attr('name', $(this).attr('href'));
    // remove attribute href
   $(this).removeAttr('href');
});

请注意,我只替换了ul 之后的元素。也许你可以使用一些更好的选择。例如:div.content a[href] 选择具有类内容的 div 内的所有锚点。

JsFiddle

【讨论】:

    【解决方案3】:

    我会使用如下所示的 jQuery 函数:

    // for every a-tag that has an href attribute
    $('a[href]').each(function() {
        // get the href attribute
        href = $(this).attr('href');
        // of the href does not start with '#'
        if (href.substr(0,1) != '#') { 
            // set the value form href to the name attribute
           $(this).attr('name', href);
            // remove the href attribute
           $(this).removeAttr('href');
        }
    });
    

    我已将解释放在 cmets 中,但如果您希望我详细说明,请随时询问。

    还有更新的小提琴:http://jsfiddle.net/ufRxr/8/

    【讨论】:

      【解决方案4】:

      应该这样做

      http://jsfiddle.net/ufRxr/10/

      $('li a').each(function(){
          $(this).attr('name', $(this).attr('href').replace('#', ''));
          $(this).removeAttr('href');
      });
      

      【讨论】:

      • 我相信你必须删除href 属性。锚点只能有 namehref 属性。
      • good catch @FelixKling 更新了它
      • @doitlikejustin 我已经在此处添加了您的更新,但不起作用:jsfiddle.net/deekster/ufRxr/13 谢谢。
      猜你喜欢
      • 2011-09-12
      • 1970-01-01
      • 2017-02-26
      • 1970-01-01
      • 2014-01-06
      • 2011-10-24
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多