Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery将href替换为name_Jquery_Replace - Fatal编程技术网

Jquery将href替换为name

Jquery将href替换为name,jquery,replace,Jquery,Replace,我有26行分散在一个页面的主体中,我需要在文件的头部通过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个实例锚链接。因此,我需要将它

我有26行分散在一个页面的主体中,我需要在文件的头部通过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

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

像这样

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

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

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

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

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

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

这样就可以了

这应该可以


我将使用如下所示的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');
    }
});
我已经在评论中给出了解释,但请随意询问是否需要我详细说明


更新后的fiddle:

我将使用一个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');
    }
});
我已经在评论中给出了解释,但请随意询问是否需要我详细说明

更新的小提琴:

这里有一个例子:

// 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内的所有锚定

这里有一个例子:

// 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内的所有锚定


您试图解决问题的(代码)是什么?
$(“a”).each(函数(){$(this).attr(“name”),$(this.attr(“href”).split(“#”)[1]);$(this.removeAttr(“href”);)
可能是使用$(selector).html()的菜单和页面导航的组合?您试图解决问题的(代码)是什么?
$(“a”).each(function(){$(this).attr(“name”,“this.”.attr(“this”).split(“#”)[1])$(this.removeAttr(“href”)})可能是和菜单以及使用$(选择器).html()的页面导航的组合?这将生成
name
值,例如
http://fiddle.jshell.net/eAkU9/show/A
(例如)
this.href
返回完整的URL,而不是
href
属性的值。@FelixKling Yup right,。修正了。你好@PSL-Thansk回复。我已经将您的代码粘贴到下面的jsfiddle,但可惜它不起作用:@user2291964您没有看到更新的fiddle并回答,请检查。您为什么不
返回this.href.substr(this.href.lastIndexOf('#')+1)?我非常怀疑访问
this.href
两次会比访问
href
两次更糟糕……这会生成
name
值,例如
http://fiddle.jshell.net/eAkU9/show/A
(例如)
this.href
返回完整的URL,而不是
href
属性的值。@FelixKling Yup right,。修正了。你好@PSL-Thansk回复。我已经将您的代码粘贴到下面的jsfiddle,但可惜它不起作用:@user2291964您没有看到更新的fiddle并回答,请检查。您为什么不
返回this.href.substr(this.href.lastIndexOf('#')+1)?我非常怀疑访问
this.href
两次会比访问
href
两次更糟糕……我相信您必须删除
href
属性。锚只能有
名称
href
属性。good catch@FelixKling更新it@doitlikejustin我在这里添加了您的更新,但不起作用:谢谢。我相信您必须删除
href
属性。锚只能有
名称
href
属性。good catch@FelixKling更新it@doitlikejustin我在这里添加了你的更新,不起作用:谢谢。