diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/api/urlsearchparams | |
parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip |
initial commit
Diffstat (limited to 'files/zh-cn/web/api/urlsearchparams')
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/append/index.html | 114 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/delete/index.html | 96 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/entries/index.html | 57 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/foreach/index.html | 87 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/get/index.html | 63 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/getall/index.html | 68 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/has/index.html | 58 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/index.html | 125 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/keys/index.html | 57 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/set/index.html | 107 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/sort/index.html | 105 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/tostring/index.html | 106 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/urlsearchparams/index.html | 133 | ||||
-rw-r--r-- | files/zh-cn/web/api/urlsearchparams/values/index.html | 57 |
14 files changed, 1233 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/urlsearchparams/append/index.html b/files/zh-cn/web/api/urlsearchparams/append/index.html new file mode 100644 index 0000000000..620c3ee5f3 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/append/index.html @@ -0,0 +1,114 @@ +--- +title: URLSearchParams.append() +slug: Web/API/URLSearchParams/append +translation_of: Web/API/URLSearchParams/append +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}} </p> + +<p><strong>append() </strong>是 {{domxref("URLSearchParams")}} 接口的一个方法。可以插入一个新搜索参数。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">URLSearchParams.append(name, value)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>name</dt> + <dd>需要插入搜索参数的键名。</dd> + <dt>value </dt> + <dd>需要插入搜索参数的值。</dd> +</dl> + +<h3 id="返回">返回</h3> + +<p>无</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">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'</pre> + +<h2 id="说明">说明</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">说明</th> + <th scope="col">现状</th> + <th scope="col">说明</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#dom-urlsearchparams-append', "append()")}}</td> + <td>{{Spec2('URL')}}</td> + <td>初始定义</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>基本支持</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>基本支持</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatChrome(49.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参见">参见</h2> + +<ul> + <li>其它URL相关的接口: {{domxref("URL")}}, {{domxref("URLUtils")}}.</li> + <li><a href="https://developers.google.com/web/updates/2016/01/urlsearchparams?hl=en">谷歌开发者:简单URL与URLSearchParams操纵</a></li> +</ul> diff --git a/files/zh-cn/web/api/urlsearchparams/delete/index.html b/files/zh-cn/web/api/urlsearchparams/delete/index.html new file mode 100644 index 0000000000..b6927e3e22 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/delete/index.html @@ -0,0 +1,96 @@ +--- +title: URLSearchParams.delete() +slug: Web/API/URLSearchParams/delete +translation_of: Web/API/URLSearchParams/delete +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}} </p> + +<p><strong>delete() </strong>是 {{domxref("URLSearchParams")}} 接口的一个方法。可以删除指定名称的所有搜索参数。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">URLSearchParams.delete(name)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>name</dt> + <dd>需要删除的键值名称</dd> +</dl> + +<h3 id="返回">返回</h3> + +<p>无</p> + +<h2 id="说明">说明</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">说明</th> + <th scope="col">状态</th> + <th scope="col">说明</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#dom-urlsearchparams-delete', "delete()")}}</td> + <td>{{Spec2('URL')}}</td> + <td>初始定义</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>基本支持</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>基本支持</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatChrome(49.0)}}</td> + </tr> + </tbody> +</table> +</div> diff --git a/files/zh-cn/web/api/urlsearchparams/entries/index.html b/files/zh-cn/web/api/urlsearchparams/entries/index.html new file mode 100644 index 0000000000..23e87b1b59 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/entries/index.html @@ -0,0 +1,57 @@ +--- +title: URLSearchParams.entries() +slug: Web/API/URLSearchParams/entries +tags: + - API + - Experimental + - Method + - Reference + - URL API + - URLSearchParams +translation_of: Web/API/URLSearchParams/entries +--- +<p>{{APIRef("URL API")}}{{SeeCompatTable}}</p> + +<p><code><strong>URLSearchParams.entries()</strong></code>方法返回一个{{jsxref("Iteration_protocols",'iterator')}},允许遍历该对象中包含的所有键/值对。每一组键值对都是 {{domxref("USVString")}}对象</p> + +<div class="note"> +<p><strong>注意</strong>: 该方法在 <a href="/en-US/docs/Web/API/Web_Workers_API">Web Workers</a>也是可用的.</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">searchParams.entries();</pre> + +<h3 id="返回值">返回值</h3> + +<p>返回一个 {{jsxref("Iteration_protocols","iterator")}}.</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js;highlight:[5]">// 创建一个测试用 URLSearchParams 对象 +var searchParams = new URLSearchParams("key1=value1&key2=value2"); + +// 显示键/值对 +for(var pair of searchParams.entries()) { + console.log(pair[0]+ ', '+ pair[1]); +} +</pre> + +<p>结果如下:</p> + +<pre>key1, value1 +key2, value2</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> +<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.</div> + +<p>{{Compat("api.URLSearchParams.entries")}}</p> +</div> + +<h2 id="另请参阅">另请参阅</h2> + +<ul> + <li>其他 URL相关接口: {{domxref("URL")}}, {{domxref("URLUtils")}}.</li> +</ul> diff --git a/files/zh-cn/web/api/urlsearchparams/foreach/index.html b/files/zh-cn/web/api/urlsearchparams/foreach/index.html new file mode 100644 index 0000000000..7ce04858eb --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/foreach/index.html @@ -0,0 +1,87 @@ +--- +title: URLSearchParams.forEach() +slug: Web/API/URLSearchParams/forEach +translation_of: Web/API/URLSearchParams/forEach +--- +<div>{{APIRef("URL API")}}</div> + +<div></div> + +<p> URLSearchParams的实例对象上的方法forEach允许通过回调函数来遍历URLSearchParams实例对象上的键值对</p> + +<p>{{availableinworkers}}</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">searchParams.forEach(callback(value,key,searchParams));</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>回调函数</dt> + <dd> 该回调函数可以接收到3个参数value,key,searchParams,我们可以在回调函数中对接收到的参数进行处理。而三个参数的含义如下:</dd> + <dt> 1. value</dt> + <dd> 当前遍历到的键值</dd> + <dt> 2. key</dt> + <dd> 当前遍历到的键名</dd> + <dt> 3. searchParams</dt> + <dd> 当前调用forEach方法的实例对象</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p> 空</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js;highlight:[5]">// 创建URLSearchParams对象的实例对象,用于测试 +var searchParams = new URLSearchParams("key1=value1&key2=value2"); + +let returnValue = searchParams.forEach(function(value, key,searchParams) { + // 打印值 + console.log(value, key,searchParams); +}); + +// 输出返回值 +console.log(returnValue) +</pre> + +<p>结果是:</p> + +<pre>value1 key1 当前调用forEach方法的实例对象(也就是searchParams) +value2 key2 当前调用forEach方法的实例对象(也就是searchParams) +undefined // 即没有返回值 +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#interface-urlsearchparams', "forEach() (see \"iterable\")")}}</td> + <td>{{Spec2('URL')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div> + + +<p>{{Compat("api.URLSearchParams.forEach")}}</p> +</div> + +<h2 id="See_also">See also</h2> + +<ul> + <li>The {{domxref("URL")}} interface.</li> +</ul> 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 +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}} </p> + +<p>{{domxref("URLSearchParams")}} 接口的<strong>get()</strong>方法返回第一个与搜索参数对应的值<strong>。</strong></p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">URLSearchParams.get(name)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>键名</dt> + <dd>将要返回的参数的键名。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>返回一个 {{domxref("USVString")}} ;如果没找到,返回 <code><strong>null</strong></code>.</p> + +<h2 id="示例">示例</h2> + +<p>如果一个页面的URL是 <code>https://example.com/?name=Jonathan&age=18</code> ,你可以这样解析参数“name”和“age”:</p> + +<pre class="default prettyprint prettyprinted"><code>let params = new URLSearchParams(document.location.search.substring(1)); +let name = params.get("name")<span class="kwd">; // is the string "Jonathan" +</span>let age = parseInt(params.get("age"), 10<span class="kwd">); // is the number 18</span></code> +</pre> + +<p>查找一个不存在的键名则返回 <code><strong>null</strong></code>:</p> + +<pre><code>let address = params.get("address"); // null</code></pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">说明</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#dom-urlsearchparams-get', "get()")}}</td> + <td>{{Spec2('URL')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.URLSearchParams.get")}}</p> +</div> diff --git a/files/zh-cn/web/api/urlsearchparams/getall/index.html b/files/zh-cn/web/api/urlsearchparams/getall/index.html new file mode 100644 index 0000000000..5fa3efdce2 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/getall/index.html @@ -0,0 +1,68 @@ +--- +title: URLSearchParams.getAll() +slug: Web/API/URLSearchParams/getAll +tags: + - API + - Experimental + - Method + - URL API + - URLSearchParams + - getAll +translation_of: Web/API/URLSearchParams/getAll +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}} </p> + +<p>{{domxref("URLSearchParams")}}接口的getAll()方法,以数组的形式返回与指定搜索参数对应的所有值。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">URLSearchParams.getAll(name)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>name</dt> + <dd>要返回的参数的名称。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个{{domxref("USVString")}}数组。</p> + +<h2 id="例子">例子</h2> + +<pre>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"]. +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">说明</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#dom-urlsearchparams-getall', "getAll()")}}</td> + <td>{{Spec2('URL')}}</td> + <td>初始定义</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.URLSearchParams.getAll")}}</p> +</div> diff --git a/files/zh-cn/web/api/urlsearchparams/has/index.html b/files/zh-cn/web/api/urlsearchparams/has/index.html new file mode 100644 index 0000000000..a4308386f1 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/has/index.html @@ -0,0 +1,58 @@ +--- +title: URLSearchParams.has() +slug: Web/API/URLSearchParams/has +translation_of: Web/API/URLSearchParams/has +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}} </p> + +<p>{{domxref("URLSearchParams")}} 的<strong>has() </strong>方法返回一个 {{jsxref("Boolean")}} 表示一个指定的键名对应的值是否存在。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>hasName</em> = URLSearchParams.has(<em>name</em>)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>键名</dt> + <dd>要查找的参数的键名。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个 {{jsxref("Boolean")}}.</p> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">let url = new URL('https://example.com?foo=1&bar=2'); +let params = new URLSearchParams(url.search.slice(1)); + +params.has('bar') === true; //true +</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">规范</th> + <th scope="col">状态</th> + <th scope="col">说明</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#dom-urlsearchparams-has', "has()")}}</td> + <td>{{Spec2('URL')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.URLSearchParams.has")}}</p> +</div> diff --git a/files/zh-cn/web/api/urlsearchparams/index.html b/files/zh-cn/web/api/urlsearchparams/index.html new file mode 100644 index 0000000000..0c21454e54 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/index.html @@ -0,0 +1,125 @@ +--- +title: URLSearchParams +slug: Web/API/URLSearchParams +tags: + - URL API + - URLSearchParams +translation_of: Web/API/URLSearchParams +--- +<p>{{ApiRef("URL API")}}</p> + +<p><strong><code>URLSearchParams</code></strong> 接口定义了一些实用的方法来处理 URL 的查询字符串。</p> + +<p>一个实现了 <code>URLSearchParams</code> 的对象可以直接用在 {{jsxref("Statements/for...of", "for...of")}} 结构中,例如下面两行是相等的:</p> + +<pre class="brush: js">for (const [key, value] of mySearchParams) {} +for (const [key, value] of mySearchParams.entries()) {}</pre> + +<p>{{availableinworkers}}</p> + +<h2 id="构造函数">构造函数</h2> + +<dl> + <dt>{{domxref("URLSearchParams.URLSearchParams", 'URLSearchParams()')}}</dt> + <dd>返回一个 <code>URLSearchParams </code>对象。</dd> +</dl> + +<h2 id="方法">方法</h2> + +<p>该接口不继承任何属性。</p> + +<dl> + <dt>{{domxref("URLSearchParams.append()")}}</dt> + <dd> 插入一个指定的键/值对作为新的搜索参数。</dd> + <dt>{{domxref("URLSearchParams.delete()")}}</dt> + <dd> 从搜索参数列表里删除指定的搜索参数及其对应的值。</dd> + <dt>{{domxref("URLSearchParams.entries()")}}</dt> + <dd> 返回一个{{jsxref("Iteration_protocols","iterator")}}可以遍历所有键/值对的对象。</dd> + <dt>{{domxref("URLSearchParams.get()")}}</dt> + <dd> 获取指定搜索参数的第一个值。</dd> + <dt>{{domxref("URLSearchParams.getAll()")}}</dt> + <dd> 获取指定搜索参数的所有值,返回是一个数组。</dd> + <dt>{{domxref("URLSearchParams.has()")}}</dt> + <dd> 返回 {{jsxref("Boolean")}} 判断是否存在此搜索参数。</dd> + <dt>{{domxref("URLSearchParams.keys()")}}</dt> + <dd>返回{{jsxref("Iteration_protocols", "iterator")}} 此对象包含了键/值对的所有键名。</dd> + <dt>{{domxref("URLSearchParams.set()")}}</dt> + <dd> 设置一个搜索参数的新值,假如原来有多个值将删除其他所有的值。</dd> + <dt>{{domxref("URLSearchParams.sort()")}}</dt> + <dd> 按键名排序。</dd> + <dt>{{domxref("URLSearchParams.toString()")}}</dt> + <dd> 返回搜索参数组成的字符串,可直接使用在URL上。</dd> + <dt>{{domxref("URLSearchParams.values()")}}</dt> + <dd> 返回{{jsxref("Iteration_protocols", "iterator")}} 此对象包含了键/值对的所有值。</dd> +</dl> + +<h2 id="示例">示例</h2> + +<pre class="brush: js">var paramsString = "q=URLUtils.searchParams&topic=api" +var searchParams = new URLSearchParams(paramsString); + +for (let p of searchParams) { + console.log(p); +} + +searchParams.has("topic") === true; // true +searchParams.get("topic") === "api"; // true +searchParams.getAll("topic"); // ["api"] +searchParams.get("foo") === null; // true +searchParams.append("topic", "webdev"); +searchParams.toString(); // "q=URLUtils.searchParams&topic=api&topic=webdev" +searchParams.set("topic", "More webdev"); +searchParams.toString(); // "q=URLUtils.searchParams&topic=More+webdev" +searchParams.delete("topic"); +searchParams.toString(); // "q=URLUtils.searchParams"</pre> + +<h3 id="Gotchas">Gotchas</h3> + +<p><code>URLSearchParams</code> 构造函数<em>不会</em>解析完整 URL,但是如果字符串起始位置有 <code>?</code> 的话会被去除。</p> + +<pre class="brush: js">var paramsString1 = "http://example.com/search?query=%40"; +var searchParams1 = new URLSearchParams(paramsString1); + +searchParams1.has("query"); // false +searchParams1.has("http://example.com/search?query"); // true + +searchParams1.get("query"); // null +searchParams1.get("http://example.com/search?query"); // "@" (equivalent to decodeURIComponent('%40')) + +var paramsString2 = "?query=value"; +var searchParams2 = new URLSearchParams(paramsString2); +searchParams2.has("query"); // true + +var url = new URL("http://example.com/search?query=%40"); +var searchParams3 = new URLSearchParams(url.search); +searchParams3.has("query") // true</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#urlsearchparams', "URLSearchParams")}}</td> + <td>{{Spec2('URL')}}</td> + <td>初次定义</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<p>{{Compat("api.URLSearchParams")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>The {{domxref("URL")}} interface.</li> + <li><a href="https://developers.google.com/web/updates/2016/01/urlsearchparams?hl=en">Google Developers: Easy URL manipulation with URLSearchParams</a></li> +</ul> diff --git a/files/zh-cn/web/api/urlsearchparams/keys/index.html b/files/zh-cn/web/api/urlsearchparams/keys/index.html new file mode 100644 index 0000000000..2ff71529b3 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/keys/index.html @@ -0,0 +1,57 @@ +--- +title: URLSearchParams.keys() +slug: Web/API/URLSearchParams/keys +tags: + - API + - Experimental + - Method + - Reference + - URL API + - URLSearchParams +translation_of: Web/API/URLSearchParams/keys +--- +<p>{{APIRef("URL API")}}{{SeeCompatTable}}</p> + +<p>URLSearchParams.keys()返回一个{{jsxref("Iteration_protocols",'iterator')}},遍历器允许遍历对象中包含的所有键。这些键都是{{domxref("USVString")}}对象。</p> + +<div class="note"> +<p><strong>注意</strong>: 该方法在 <a href="/en-US/docs/Web/API/Web_Workers_API">Web Workers</a>中也可使用</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">searchParams.keys();</pre> + +<h3 id="返回值">返回值</h3> + +<p>返回一个{{jsxref("Iteration_protocols","iterator")}}.</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js;highlight:[5]">// 建立一个测试用URLSearchParams对象 +var searchParams = new URLSearchParams("key1=value1&key2=value2"); + +// 输出键值对 +for(var key of searchParams.keys()) { + console.log(key); +} +</pre> + +<p>结果如下:</p> + +<pre>key1 +key2</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.URLSearchParams.keys")}}</p> +</div> + +<h2 id="另请参考">另请参考</h2> + +<ul> + <li>其他URL相关接口: {{domxref("URL")}}, {{domxref("URLUtils")}}.</li> +</ul> diff --git a/files/zh-cn/web/api/urlsearchparams/set/index.html b/files/zh-cn/web/api/urlsearchparams/set/index.html new file mode 100644 index 0000000000..6790f615e7 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/set/index.html @@ -0,0 +1,107 @@ +--- +title: URLSearchParams.set() +slug: Web/API/URLSearchParams/set +translation_of: Web/API/URLSearchParams/set +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}} </p> + +<p>{{domxref("URLSearchParams")}}接口的set()方法用于设置和搜索参数相关联的值。如果设置前已经存在匹配的值,该方法会删除多余的,如果将要设置的值不存在,则创建它</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">URLSearchParams.set(name, value)</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt>键名</dt> + <dd>将要设置的参数的健值名。</dd> + <dt>参数值</dt> + <dd>所要设置的参数值。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>Void</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js">let url = new URL('https://example.com?foo=1&bar=2'); +let params = new URLSearchParams(url.search.slice(1)); + +//Add a third parameter. +params.set('baz', 3); +</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#dom-urlsearchparams-set', "set()")}}</td> + <td>{{Spec2('URL')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatChrome(49.0)}}</td> + </tr> + </tbody> +</table> +</div> diff --git a/files/zh-cn/web/api/urlsearchparams/sort/index.html b/files/zh-cn/web/api/urlsearchparams/sort/index.html new file mode 100644 index 0000000000..abe8fecd01 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/sort/index.html @@ -0,0 +1,105 @@ +--- +title: URLSearchParams.sort() +slug: Web/API/URLSearchParams/sort +translation_of: Web/API/URLSearchParams/sort +--- +<p>{{APIRef("URL API")}}{{SeeCompatTable}}</p> + +<p><code><strong>URLSearchParams.sort()</strong></code> 方法对包含在此对象中的所有键/值对进行排序,并返回undefined。排序顺序是根据键的Unicode代码点。该方法使用稳定的排序算法 (即,将保留具有相等键的键/值对之间的相对顺序)。</p> + +<h2 id="Syntax">Syntax</h2> + +<pre class="syntaxbox">searchParams.sort();</pre> + +<h3 id="Return_value">Return value</h3> + +<p>Returns <code>undefined</code>.</p> + +<h2 id="Example">Example</h2> + +<pre class="brush: js;highlight:[5]">// Create a test URLSearchParams object +var searchParams = new URLSearchParams("c=4&a=2&b=3&a=1"); + +// Sort the key/value pairs +searchParams.sort(); + +// Display the sorted query string +console.log(searchParams.toString()); +</pre> + +<p>The result is:</p> + +<pre>a=2&a=1&b=3&c=4</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <tbody> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + <tr> + <td>{{SpecName('URL', '#urlsearchparams','sort() (as iterator<>)')}}</td> + <td>{{Spec2('URL')}}</td> + <td>Initial definition</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>Firefox OS (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatNo}}</td> + </tr> + </tbody> +</table> +</div> diff --git a/files/zh-cn/web/api/urlsearchparams/tostring/index.html b/files/zh-cn/web/api/urlsearchparams/tostring/index.html new file mode 100644 index 0000000000..6efd11d077 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/tostring/index.html @@ -0,0 +1,106 @@ +--- +title: URLSearchParams.toString() +slug: Web/API/URLSearchParams/toString +translation_of: Web/API/URLSearchParams/toString +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}} </p> + +<p>{{domxref("URLSearchParams")}} 接口的toString()方法 返回适用在URL中的查询字符串。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">URLSearchParams.toString()</pre> + +<h3 id="返回值">返回值</h3> + +<p>字符串</p> + +<h2 id="实例">实例</h2> + +<pre class="brush: js">let url = new URL('https://example.com?foo=1&bar=2'); +let params = new URLSearchParams(url.search.slice(1)); + +//Add a second foo parameter. +params.append('foo', 4); +console.log(params.toString()); +//Prints 'foo=1&bar=2&foo=4'.</pre> + +<h2 id="规范">规范</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('URL', '#urlsearchparams-stringification-behavior', "toString()")}}</td> + <td>{{Spec2('URL')}}</td> + <td>Initial definition. Appears in the IDL as <code>stringifier</code>.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatChrome(49.0)}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="参考">参考</h2> + +<ul> + <li>Other URL-related interfaces: {{domxref("URL")}}, {{domxref("URLUtils")}}.</li> + <li><a href="https://developers.google.com/web/updates/2016/01/urlsearchparams?hl=en">Google Developers: Easy URL manipulation with URLSearchParams</a></li> +</ul> diff --git a/files/zh-cn/web/api/urlsearchparams/urlsearchparams/index.html b/files/zh-cn/web/api/urlsearchparams/urlsearchparams/index.html new file mode 100644 index 0000000000..7d4c0b206b --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/urlsearchparams/index.html @@ -0,0 +1,133 @@ +--- +title: URLSearchParams() +slug: Web/API/URLSearchParams/URLSearchParams +translation_of: Web/API/URLSearchParams/URLSearchParams +--- +<p>{{ApiRef("URL API")}}{{SeeCompatTable}}</p> + +<p><code><strong>URLSearchParams()</strong></code> 构造器创建并返回一个新的{{domxref("URLSearchParams")}} 对象。 开头的<code>'?'</code> 字符会被忽略。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">var <em>URLSearchParams</em> = new URLSearchParams(<em>init</em>);</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><em><code>init</code></em> {{optional_inline}}</dt> + <dd>一个 {{domxref("USVString")}} 实例,一个 {{domxref("URLSearchParams")}} 实例,一个 {{domxref("USVString")}},或者一个包含 {{domxref("USVString")}} 的记录。注意使用一个 <code>URLSearchParams</code> 实例作为参数已经被弃用了,以后的浏览器将会只使用一个 <code>USVString</code> 作为参数。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>一个 {{domxref("URLSearchParams")}} 实例。</p> + +<h2 id="例子">例子</h2> + +<p>下面的例子展示了用一个URL字符串创建一个 {{domxref("URLSearchParams")}} 对象。</p> + +<pre class="brush: js">// Pass in a string literal +var url = new URL('https://example.com?foo=1&bar=2'); +// Retrieve from window.location +var url2 = new URL(window.location); + +// Retrieve params via url.search, passed into ctor +var params = new URLSearchParams(url.search); +var params2 = new URLSearchParams(url2.search); + +// Pass in a sequence +var params3 = new URLSearchParams([["foo", 1],["bar", 2]]); + +// Pass in a record +var params4 = new URLSearchParams({"foo" : 1 , "bar" : 2}); +</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari (WebKit)</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatGeckoDesktop("29.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td><code>USVString</code> or sequence for <code>init</code> object</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoDesktop("53")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + <tr> + <td>Record for <code>init</code> object</td> + <td>{{CompatNo}}</td> + <td>{{CompatGeckoDesktop("54")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Android Webview</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + <th>Chrome for Android</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatNo}}</td> + <td>{{CompatChrome(49.0)}}</td> + <td>{{CompatGeckoMobile("29.0")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatChrome(49.0)}}</td> + </tr> + <tr> + <td><code>USVString</code> of sequence for <code>init</code> object</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("53")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + <tr> + <td>Record for <code>init</code> object</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile("54")}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatVersionUnknown}}</td> + </tr> + </tbody> +</table> +</div> diff --git a/files/zh-cn/web/api/urlsearchparams/values/index.html b/files/zh-cn/web/api/urlsearchparams/values/index.html new file mode 100644 index 0000000000..a9e476bc83 --- /dev/null +++ b/files/zh-cn/web/api/urlsearchparams/values/index.html @@ -0,0 +1,57 @@ +--- +title: URLSearchParams.values() +slug: Web/API/URLSearchParams/values +tags: + - API + - Experimental + - Iterator + - Method + - Reference + - URL API + - URLSearchParams +translation_of: Web/API/URLSearchParams/values +--- +<p>{{APIRef("URL API")}}</p> + +<p>URLSearchParams.values()方法返回一个{{jsxref("Iteration_protocols",'iterator')}},该遍历器允许遍历对象中包含的所有值。这些值都是{{domxref("USVString")}}对象。</p> + +<div class="note"> +<p><strong>注意</strong>: 该方法在<a href="/en-US/docs/Web/API/Web_Workers_API">Web Workers</a>中也可使用</p> +</div> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">searchParams.values();</pre> + +<h3 id="返回值">返回值</h3> + +<p>返回一个{{jsxref("Iteration_protocols","iterator")}}.</p> + +<h2 id="例子">例子</h2> + +<pre class="brush: js;highlight:[5]">// 创建一个测试用URLSearchParams对象 +var searchParams = new URLSearchParams("key1=value1&key2=value2"); + +// 输出值 +for(var value of searchParams.values()) { + console.log(value); +}</pre> + +<p>结果如下:</p> + +<pre>value1 +value2</pre> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + +<div> + + +<p>{{Compat("api.URLSearchParams.values")}}</p> +</div> + +<h2 id="另请参考">另请参考</h2> + +<ul> + <li>其他URL相关接口: {{domxref("URL")}}, {{domxref("URLUtils")}}.</li> +</ul> |