我将href
包装到URL,并尝试删除一个组件,与delete
一起删除,但组件并未消失。你知道为什么吗?
let url = new URL(window.location.href);
let p = url.searchParams['postId'+$(".selected").length];
delete p;
window.location = url.toString();
我试过了:
const filteredItems = url.searchParams.filter(key => url.searchParams[key] == postID);
let key = filteredItems.keys.first;
url.searchParams.delete(key);
但是上面写着
Uncaught TypeError: url.searchParams.filter is not a function
我现在试过这个表达,但filter
不工作,你知道为什么吗?
function togglePost(postID) {
let url = new URL(window.location.href);
const filteredItems = Object.keys(url.searchParams).filter(key =>
url.searchParams[key] == postID
);
let key = filteredItems.keys.first;
delete
操作符从对象中删除属性。
您正在尝试删除一个变量。
要从URLSearchParams对象中删除某些内容,请使用delete
方法:
let url = new URL('http://example.com/foo.cgi?a=1&b=2');
console.log(url.toString());
url.searchParams.delete('a');
console.log(url.toString());