gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 22:29:19 27 4
gpt4 key购买 nike

我正在尝试使用每种方法更改 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com