我正在尝试使用每种方法更改 href,
here is demo, inspect a, you'll see there is no change
html:
<a href="#/news">News</a>
<a href="#/news/detail">Detail</a>
<a href="#/sport">Sport</a>
<a href="#/sport/football">Football</a>
jQuery:
$('a').each(function() {
$(this).attr('href').replace('#/',''); //tried to erase #/ from all hrefs
});
最佳答案
您发布的代码将获取字符串
形式的值,然后正确地替换
这些值,但它会立即丢弃结果。您需要将替换后的值传递给attr
。尝试以下方法
$('a').each(function() {
var value = $(this).attr('href');
$(this).attr('href', value.replace('#/',''));
});
关于jquery - 如何用每个 ?//strip url 替换元素的 attr href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11369182/