构造函数
new URLSearchParams('https://example.com?foo=1&bar=2')
实例属性
size
实例方法
append()
let url = new URL('https://example.com?foo=1&bar=2');
let params = new URLSearchParams(url.search.slice(1));
//添加第二个 foo 搜索参数。
params.append('foo', 4);
//查询字符串变成:'foo=1&bar=2&foo=4'
delete()
let url = new URL('https://example.com?foo=1&bar=2');
let params = new URLSearchParams();
//参数:name
params.delete('foo');
entries()
// 创建一个测试用 URLSearchParams 对象
var searchParams = new URLSearchParams("key1=value1&key2=value2");
// 显示键/值对
for(var pair of searchParams.entries()) {
console.log(pair[0]+ ', '+ pair[1]);
}
// 结果
key1, value1
key2, value2
get()
let params = new URLSearchParams(document.location.search.substring(1));
let name = params.get("name"); // is the string "Jonathan"
let age = parseInt(params.get("age"), 10); // is the number 18
getAll()
let url = new URL('https://example.com?foo=1&bar=2');
let params = new URLSearchParams(url.search.slice(1));
//为 foo 参数添加第二个值
params.append('foo', 4);
console.log(params.getAll('foo')) //输出 ["1","4"]
接口参考地址: