只是我有一堆像这样的链接
<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>
Run Code Online (Sandbox Code Playgroud)
然而,我希望能够做的是浏览所有这些链接并删除结尾位“&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>
Run Code Online (Sandbox Code Playgroud)
所以我正在研究看起来像这样的函数:
function removeCategory(){
$('#categoryLinks a').each(function(){
// as you can see, i don't know what goes in here!
});
}
Run Code Online (Sandbox Code Playgroud)
我知道如何做相反的事情,即在 href 中附加一个类别,但就将其删除而言,我画了一个空白。
我该怎么做呢?
这将删除所有参数
$('a[href*=?]').each(function () {
var href = $(this).attr('href');
$(this).attr('href', href.substring(0, href.indexOf('?'));
});
Run Code Online (Sandbox Code Playgroud)
这将剥离特定的一个
$("a[href*='category=']").each(function () {
var href = $(this).attr('href');
$(this).attr('href', href.replace(/&?category=\d+/, ''));
});
Run Code Online (Sandbox Code Playgroud)
a[href*='category=']category=在标签href的属性中查找字符串a。然后这个属性以及相应的值被替换为空字符串。
| 归档时间: |
|
| 查看次数: |
3211 次 |
| 最近记录: |