From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/urlsearchparams/get/index.html | 63 ++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 files/zh-cn/web/api/urlsearchparams/get/index.html (limited to 'files/zh-cn/web/api/urlsearchparams/get') diff --git a/files/zh-cn/web/api/urlsearchparams/get/index.html b/files/zh-cn/web/api/urlsearchparams/get/index.html new file mode 100644 index 0000000000..232b200c83 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/get/index.html @@ -0,0 +1,63 @@ +--- +title: URLSearchParams.get() +slug: Web/API/URLSearchParams/get +translation_of: Web/API/URLSearchParams/get +--- +

{{ApiRef("URL API")}}{{SeeCompatTable}} 

+ +

{{domxref("URLSearchParams")}} 接口的get()方法返回第一个与搜索参数对应的值

+ +

语法

+ +
URLSearchParams.get(name)
+ +

参数

+ +
+
键名
+
将要返回的参数的键名。
+
+ +

返回值

+ +

返回一个 {{domxref("USVString")}} ;如果没找到,返回 null.

+ +

示例

+ +

如果一个页面的URL是 https://example.com/?name=Jonathan&age=18 ,你可以这样解析参数“name”和“age”:

+ +
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
+
+ +

查找一个不存在的键名则返回 null:

+ +
let address = params.get("address"); // null
+ +

规范

+ + + + + + + + + + + + + + + + +
规范状态说明
{{SpecName('URL', '#dom-urlsearchparams-get', "get()")}}{{Spec2('URL')}}Initial definition.
+ +

浏览器兼容性

+ +
+ + +

{{Compat("api.URLSearchParams.get")}}

+
-- cgit v1.2.3-54-g00ecf