Javascript 将类从列表附加到URL

Javascript 将类从列表附加到URL,javascript,jquery,Javascript,Jquery,我有一份无序的名单。我想从URL后面的每个元素追加类。但是我怎么能做到呢 这是我的密码: $(“li”)。每个(函数(索引){ var newClass=$(this.attr('class'); console.log(newClass); }); 您可以使用函数更新锚定的href 在本例中,我使用了重载的.attr(attributeName,function),它使用函数返回的值设置href属性 $(函数(){ $(“li a”).attr('href',function(\

我有一份无序的名单。我想从URL后面的每个元素追加类。但是我怎么能做到呢

这是我的密码:

$(“li”)。每个(函数(索引){
var newClass=$(this.attr('class');
console.log(newClass);
});

您可以使用函数更新锚定的
href

在本例中,我使用了重载的
.attr(attributeName,function)
,它使用函数返回的值设置
href
属性

$(函数(){
$(“li a”).attr('href',function(\ux,href){
var newClass=$(this.nexist('li').attr('class');
return href+'#'+newClass;
});
})

返回href+'#'+newClass如果@hersaidy对#