简而言之,我有很多这样的链接
<div id="categoryLinks">
<a href="blah.php?something=this&category=21"></a>
<a href="blah.php?something=that&category=21"></a>
<a href="blah.php?something=then&category=21"></a>
</div>
但是,我想做的是浏览所有这些链接,并删除我应选择的末尾位'&category = 21',这样它们都将看起来像:
<div id="categoryLinks">
<a href="blah.php?something=this"></a>
<a href="blah.php?something=that"></a>
<a href="blah.php?something=then"></a>
</div>
所以我正在研究看起来像这样的函数:
function removeCategory(){
$('#categoryLinks a').each(function(){
// as you can see, i don't know what goes in here!
});
}
我知道如何做相反的事情,这是将类别添加到href,但是就删除它而言,我还是空白。
我该怎么做呢?
最佳答案
这将剥离所有参数
$('a[href*=?]').each(function () {
var href = $(this).attr('href');
$(this).attr('href', href.substring(0, href.indexOf('?'));
});
这将剥离特定的
$("a[href*='category=']").each(function () {
var href = $(this).attr('href');
$(this).attr('href', href.replace(/&?category=\d+/, ''));
});
a[href*='category=']
在category=
标记的href
属性中查找a
字符串。然后,将这个属性以及对应的值替换为空字符串。关于jquery - 如何使用jQuery从页面上的链接中删除变量,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1187238/