如何用每个替换元素的attr href? // strip url

我试图用每种方法改变href,

这是演示,检查一下,你会看到没有变化

HTML:

News Detail Sport Football​​​​​​​​​​​​ 

jQuery的:

 $('a').each(function() { $(this).attr('href').replace('#/',''); //tried to erase #/ from all hrefs });​ 

您发布的代码将获取值作为string然后正确replace值但它会立即丢弃结果。 您需要将替换值传递给attr 。 请尝试以下方法

 $('a').each(function() { var value = $(this).attr('href'); $(this).attr('href', value.replace('#/','')); });​ 
 var href = $(this).attr('href'); $(this).attr('href', href.replace('#/','')); 

您还可以检查href值并创建条件