aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/document/activeelement/index.html
diff options
context:
space:
mode:
authorFlorian Merz <me@fiji-flo.de>2021-02-23 15:56:25 +0100
committerFlorian Merz <me@fiji-flo.de>2021-02-23 15:56:25 +0100
commit05b2650717b84d199f6ebd5df8a8f9d1e3b23f3f (patch)
tree3a996b21e1eb1ed852ab792b9752648783a87a75 /files/zh-cn/web/api/document/activeelement/index.html
parent10701ffef1cb57f06970990b9b7086ea7f24a019 (diff)
downloadtranslated-content-05b2650717b84d199f6ebd5df8a8f9d1e3b23f3f.tar.gz
translated-content-05b2650717b84d199f6ebd5df8a8f9d1e3b23f3f.tar.bz2
translated-content-05b2650717b84d199f6ebd5df8a8f9d1e3b23f3f.zip
sync: move
Diffstat (limited to 'files/zh-cn/web/api/document/activeelement/index.html')
-rw-r--r--files/zh-cn/web/api/document/activeelement/index.html86
1 files changed, 86 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/document/activeelement/index.html b/files/zh-cn/web/api/document/activeelement/index.html
new file mode 100644
index 0000000000..90b67fbf5c
--- /dev/null
+++ b/files/zh-cn/web/api/document/activeelement/index.html
@@ -0,0 +1,86 @@
+---
+title: DocumentOrShadowRoot.activeElement
+slug: Web/API/DocumentOrShadowRoot/activeElement
+translation_of: Web/API/DocumentOrShadowRoot/activeElement
+---
+<div>{{APIRef("Shadow DOM")}}</div>
+
+<p><span class="seoSummary">{{domxref("Document")}} 和 {{domxref("ShadowRoot")}} 接口的 <strong><code>activeElement</code></strong> 只读属性,用来返回当前在DOM或者shadow DOM树中处于聚焦状态的{{domxref("Element")}}。</span></p>
+
+<p>通常情况下,如果 {{domxref("HTMLInputElement")}} 或者 {{domxref("HTMLTextAreaElement")}}元素中有文字被选中时, <code>activeElement</code>属性就会返回该元素 。这时,你可以调用该元素的{{domxref("Document.selectionStart", "selectionStart")}} 和 {{domxref("Document.selectionEnd", "selectionEnd")}} 属性获取更多选中文字的信息. 其他情况下,焦点元素也可能是{{HTMLElement("select")}}元素 (menu) 或者一个别的 {{HTMLElement("input")}} 元素, 比如 <code>"button"</code>, <code>"checkbox"</code>, 或者 <code>"radio"</code>.</p>
+
+<p>通常用户可以使用tab键来切换页面中的焦点元素获得焦点, 使用空格键使元素active (比如按下一个按钮或者 切换一个 radio). 具体哪些元素可以获得焦点与系统和浏览器的设置有关。比如, 在 macOS 系统上, 不是text input元素默认情况下是不能获得焦点的。</p>
+
+<div class="note">
+<p><strong>Note:</strong> Focus (可收到input事件的元素) 和 selection (目前页面被高亮的部分)不是一回事. 你可以通过 {{domxref("window.getSelection()")}}获取当前用户选择的文本.</p>
+</div>
+
+<h2 id="Syntax">Syntax</h2>
+
+<pre class="syntaxbox notranslate"><em>element</em> = <em>DocumentOrShadowRoot</em>.activeElement</pre>
+
+<h3 id="Value">Value</h3>
+
+<p>当前获得焦点的 {{domxref('Element')}} ,如果没有焦点元素,会返回 {{HTMLElement("body")}} 或者 <code>null</code> 。</p>
+
+<h2 id="Example">Example</h2>
+
+<h3 id="HTML">HTML</h3>
+
+<pre class="brush: html notranslate">&lt;p&gt;Select some text from one of the text areas below:&lt;/p&gt;
+
+&lt;form&gt;
+ &lt;textarea name="ta-example-one" id="ta-example-one" rows="7" cols="40"&gt;This is Text Area One. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec tincidunt, lorem a porttitor molestie, odio nibh iaculis libero, et accumsan nunc orci eu dui.&lt;/textarea&gt;
+ &lt;textarea name="ta-example-two" id="ta-example-two" rows="7" cols="40"&gt;This is Text Area Two. Fusce ullamcorper, nisl ac porttitor adipiscing, urna orci egestas libero, ut accumsan orci lacus laoreet diam. Morbi sed euismod diam.&lt;/textarea&gt;
+&lt;/form&gt;
+
+&lt;p&gt;Active element ID: &lt;b id="output-element"&gt;&lt;/b&gt;&lt;/p&gt;
+&lt;p&gt;Selected text: &lt;b id="output-text"&gt;&lt;/b&gt;&lt;/p&gt;</pre>
+
+<h3 id="JavaScript">JavaScript</h3>
+
+<pre class="brush: js notranslate">function onMouseUp(e) {
+ const activeTextarea = document.activeElement;
+ const selection = activeTextarea.value.substring(
+ activeTextarea.selectionStart, activeTextarea.selectionEnd
+ );
+
+ const outputElement = document.getElementById('output-element');
+ const outputText = document.getElementById('output-text');
+ outputElement.innerHTML = activeTextarea.id;
+ outputText.innerHTML = selection;
+}
+
+const textarea1 = document.getElementById('ta-example-one');
+const textarea2 = document.getElementById('ta-example-two');
+textarea1.addEventListener('mouseup', onMouseUp, false);
+textarea2.addEventListener('mouseup', onMouseUp, false);</pre>
+
+<h3 id="Result">Result</h3>
+
+<p>{{ EmbedLiveSample('Example', '400', '400') }}</p>
+
+<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('HTML WHATWG', 'interaction.html#dom-document-activeelement', 'activeElement')}}</td>
+ <td>{{Spec2('HTML WHATWG')}}</td>
+ <td></td>
+ </tr>
+ </tbody>
+</table>
+
+<h2 id="Browser_compatibility">Browser compatibility</h2>
+
+<div>
+
+
+<p>{{Compat("api.DocumentOrShadowRoot.activeElement")}}</p>
+</div>