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/navigator/share/index.html | |
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/navigator/share/index.html')
-rw-r--r-- | files/zh-cn/web/api/navigator/share/index.html | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/navigator/share/index.html b/files/zh-cn/web/api/navigator/share/index.html new file mode 100644 index 0000000000..3a0f14dba3 --- /dev/null +++ b/files/zh-cn/web/api/navigator/share/index.html @@ -0,0 +1,97 @@ +--- +title: Navigator.share +slug: Web/API/Navigator/share +tags: + - Navitator + - Share + - Web Share +translation_of: Web/API/Navigator/share +--- +<div>{{APIRef("HTML DOM")}}{{SeeCompatTable}} {{securecontext_header}}</div> + +<p><strong><code>Navigator.share()</code></strong><font><font>方法通过调用本机的共享机制作为Web Share API的一部分。</font><font>如果不支持Web Share API,则此方法为</font></font><code>undefined</code><font><font>。</font></font></p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox notranslate">const sharePromise = window.navigator.share(<var>data</var>); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><var>data</var></dt> + <dd><font>包含要共享的数据的对象。</font><font>必须至少指定以下字段之一。</font><font>可用选项包括:</font></dd> +</dl> + +<ul> + <li><code>url</code>: 要共享的URL( {{domxref("USVString")}} )</li> + <li><code>text</code>: 要共享的文本( {{domxref("USVString")}} )</li> + <li><code>title</code>: 要共享的标题( {{domxref("USVString")}})</li> + <li><code>files</code>: 要共享的文件(“FrozenArray”)</li> +</ul> + +<dl> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>该方法将会返回一个{{jsxref("Promise")}}。一旦用户完成分享,这个promise将会接受 。如果指定的共享数据格式不正确,promise将会立即拒绝;如果用户取消了分享,promise也会拒绝。</p> + +<p>例如, 在Android的Chrome上, 将在用户选择要共享的应用程序后将会解析共享的内容。.</p> + +<h2 id="示例">示例</h2> + +<pre class="brush: js notranslate">navigator.share({ + title: document.title, + text: 'Hello World', + url: 'https://developer.mozilla.org', +}); // 分享MDN的URL</pre> + +<h4 id="分享文件"><strong>分享文件</strong></h4> + +<p>分享文件之前,先使用<code>navigator.canShare()</code>.判断这个文件能否被分享,Then include an array of files in the call to <code>navigator.share():</code></p> + +<p>Notice: That the sample handles feature detection by testing for <code>navigator.canShare()</code> rather than for <code>navigator.share()</code>. The data object passed to <code>canShare()</code> only supports the <code>files</code> property. Image, video, audio, and text files can be shared.</p> + +<pre class="brush: js notranslate">if (navigator.canShare && navigator.canShare({ files: filesArray })) { + navigator.share({ + files: filesArray, + title: 'Pictures', + text: 'Our Pictures.', + }) + .then(() => console.log('Share was successful.')) + .catch((error) => console.log('Sharing failed', error)); +} else { + console.log(`Your system doesn't support sharing files.`); +}</pre> + +<h2 id="规范">规范</h2> + +<table> + <thead> + <tr> + <th scope="col">Specification</th> + <th scope="col">Status</th> + <th scope="col">Comment</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('Web Share API','#share-method','share()')}}</td> + <td>{{Spec2('Web Share API')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容性">浏览器兼容性</h2> + + + +<p>{{Compat("api.Navigator.share")}}</p> + +<h2 id="参见">参见</h2> + +<ul> + <li>{{domxref("navigator.canShare()")}}</li> +</ul> |