Jquery将href替换为name
我有26行分散在一个页面的主体中,我需要在文件的头部通过jquery对其进行更改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个实例锚链接。因此,我需要将它
<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我在这里添加了你的更新,不起作用:谢谢。