diff options
author | MDN <actions@users.noreply.github.com> | 2021-04-17 00:11:36 +0000 |
---|---|---|
committer | MDN <actions@users.noreply.github.com> | 2021-04-17 00:11:36 +0000 |
commit | 0ccebc7eb352eda4d26d0b876fea36f24f482eec (patch) | |
tree | f5c0a32bc5149d8bd5aa7942577758ce7c8ed485 /files/zh-cn/orphaned/web | |
parent | 40cd01daeb2f6f7fff40ff2986208513afc8678e (diff) | |
download | translated-content-0ccebc7eb352eda4d26d0b876fea36f24f482eec.tar.gz translated-content-0ccebc7eb352eda4d26d0b876fea36f24f482eec.tar.bz2 translated-content-0ccebc7eb352eda4d26d0b876fea36f24f482eec.zip |
[CRON] sync translated content
Diffstat (limited to 'files/zh-cn/orphaned/web')
3 files changed, 419 insertions, 0 deletions
diff --git a/files/zh-cn/orphaned/web/api/parentnode/append/index.html b/files/zh-cn/orphaned/web/api/parentnode/append/index.html new file mode 100644 index 0000000000..6f06f7a420 --- /dev/null +++ b/files/zh-cn/orphaned/web/api/parentnode/append/index.html @@ -0,0 +1,147 @@ +--- +title: ParentNode.append() +slug: orphaned/Web/API/ParentNode/append +tags: + - API + - DOM + - Node + - ParentNode + - Reference +translation_of: Web/API/ParentNode/append +original_slug: Web/API/ParentNode/append +--- +<div>{{APIRef("DOM")}}</div> + +<div> <strong><code>ParentNode.append</code></strong> 方法在 <code>ParentNode</code>的最后一个子节点之后插入一组 {{domxref("Node")}} 对象或 {{domxref("DOMString")}} 对象。</div> + +<div>被插入的 {{domxref("DOMString")}} 对象等价为 {{domxref("Text")}} 节点。</div> + +<div></div> + +<div>与 {{domxref("Node.appendChild()")}} 的差异:</div> + +<div></div> + +<ul> + <li><code>ParentNode.append()</code>允许追加 {{domxref("DOMString")}} 对象,而<code><font face="Open Sans, Arial, sans-serif"> </font>Node.appendChild()</code> 只接受 {{domxref("Node")}} 对象。</li> + <li><code>ParentNode.append()</code> <a href="https://repl.it/FgPh/1">没有返回值</a>,而 <code>Node.appendChild()</code> 返回追加的 {{domxref("Node")}} 对象。</li> + <li><code>ParentNode.append()</code> 可以追加多个节点和字符串,而 <code>Node.appendChild()</code> 只能追加一个节点。</li> +</ul> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">[Throws, Unscopable] +void ParentNode.append((Node or DOMString)... nodes); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>nodes</code></dt> + <dd>一组要插入的 {{domxref("Node")}} 或 {{domxref("DOMString")}} 对象。</dd> +</dl> + +<h3 id="异常">异常</h3> + +<ul> + <li>{{domxref("HierarchyRequestError")}}: 在层次结构中的指定点不能插入节点。</li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="插入一个元素节点">插入一个元素节点</h3> + +<pre class="brush: js">var parent = document.createElement("div"); +var p = document.createElement("p"); +parent.append(p); + +console.log(parent.childNodes); // NodeList [ <p> ] +</pre> + +<h3 id="插入文本">插入文本</h3> + +<pre class="brush: js">var parent = document.createElement("div"); +parent.append("Some text"); + +console.log(parent.textContent); // "Some text"</pre> + +<h3 id="插入一个节点,同时插入一些文本">插入一个节点,同时插入一些文本</h3> + +<pre class="brush: js">var parent = document.createElement("div"); +var p = document.createElement("p"); +parent.append("Some text", p); + +console.log(parent.childNodes); // NodeList [ #text "Some text", <p> ]</pre> + +<h3 id="ParentNode.append_方法在_with_语句中不生效"><code>ParentNode.append()</code> 方法在 with 语句中不生效</h3> + +<p>为了保证向后兼容,append 方法在 with 语句中会被特殊处理,详情请看 {{jsxref("Symbol.unscopables")}}。</p> + +<pre class="brush: js">var parent = document.createElement("div"); + +with(parent) { + append("foo"); +} +// ReferenceError: append is not defined </pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>下面的 Polyfill 只支持到 IE 9 及以上:</p> + +<pre class="brush: js">// Source: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/append()/append().md +(function (arr) { + arr.forEach(function (item) { + if (item.hasOwnProperty('append')) { + return; + } + Object.defineProperty(item, 'append', { + configurable: true, + enumerable: true, + writable: true, + value: function append() { + var argArr = Array.prototype.slice.call(arguments), + docFrag = document.createDocumentFragment(); + + argArr.forEach(function (argItem) { + var isNode = argItem instanceof Node; + docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); + }); + + this.appendChild(docFrag); + } + }); + }); +})([Element.prototype, Document.prototype, DocumentFragment.prototype]);</pre> + +<h2 id="规范">规范</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('DOM WHATWG', '#dom-parentnode-append', 'ParentNode.append()')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="浏览器兼容">浏览器兼容</h2> + + + +<p>{{Compat("api.ParentNode.append")}}</p> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>{{domxref("ParentNode")}} and {{domxref("ChildNode")}}</li> + <li>{{domxref("ParentNode.prepend()")}}</li> + <li>{{domxref("Node.appendChild()")}}</li> + <li>{{domxref("ChildNode.after()")}}</li> + <li>{{domxref("NodeList")}}</li> +</ul> diff --git a/files/zh-cn/orphaned/web/api/parentnode/prepend/index.html b/files/zh-cn/orphaned/web/api/parentnode/prepend/index.html new file mode 100644 index 0000000000..c2779973b0 --- /dev/null +++ b/files/zh-cn/orphaned/web/api/parentnode/prepend/index.html @@ -0,0 +1,135 @@ +--- +title: ParentNode.prepend() +slug: orphaned/Web/API/ParentNode/prepend +tags: + - API + - DOM + - Method + - Node + - ParentNode + - Reference + - prepend + - 方法 +translation_of: Web/API/ParentNode/prepend +original_slug: Web/API/ParentNode/prepend +--- +<div>{{APIRef("DOM")}}</div> + +<p><strong><code>ParentNode.prepend</code></strong> 方法可以在父节点的第一个子节点之前插入一系列{{domxref("Node")}}对象或者{{domxref("DOMString")}}对象。{{domxref("DOMString")}}会被当作{{domxref("Text")}}节点对待(也就是说插入的不是HTML代码)。</p> + +<h2 id="语法">语法</h2> + +<pre class="syntaxbox">ParentNode.prepend((Node or DOMString)... nodes); +</pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>nodes</code></dt> + <dd>要插入的一系列{{domxref("Node")}}或者{{domxref("DOMString")}}。</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p><code>undefined</code>.</p> + +<h3 id="错误">错误</h3> + +<ul> + <li>{{domxref("HierarchyRequestError")}}:节点不能插入当前层级内。</li> +</ul> + +<h2 id="例子">例子</h2> + +<h3 id="Prepending_an_element">Prepending an element</h3> + +<pre class="brush: js">var parent = document.createElement("div"); +var p = document.createElement("p"); +var span = document.createElement("span"); +parent.append(p); +parent.prepend(span); + +console.log(parent.childNodes); // NodeList [ <span>, <p> ] +</pre> + +<h3 id="Prepending_text">Prepending text</h3> + +<pre class="brush: js">var parent = document.createElement("div"); +parent.append("Some text"); +parent.prepend("Headline: "); + +console.log(parent.textContent); // "Headline: Some text"</pre> + +<h3 id="Appending_an_element_and_text">Appending an element and text</h3> + +<pre class="brush: js">var parent = document.createElement("div"); +var p = document.createElement("p"); +parent.prepend("Some text", p); + +console.log(parent.childNodes); // NodeList [ #text "Some text", <p> ]</pre> + +<h3 id="ParentNode.prepend_is_unscopable"><code>ParentNode.prepend()</code> is unscopable</h3> + +<p><code>prepend()不能在with语句内使用,详情参考</code>{{jsxref("Symbol.unscopables")}}。</p> + +<pre class="brush: js">var parent = document.createElement("div"); + +with(parent) { + prepend("foo"); +} +// ReferenceError: prepend is not defined </pre> + +<h2 id="Polyfill">Polyfill</h2> + +<p>使用下面的代码在IE9或更高版本中模拟<code>prepend()</code>方法:</p> + +<pre class="brush: js">// from: https://github.com/jserz/js_piece/blob/master/DOM/ParentNode/prepend()/prepend().md +(function (arr) { + arr.forEach(function (item) { + item.prepend = item.prepend || function () { + var argArr = Array.prototype.slice.call(arguments), + docFrag = document.createDocumentFragment(); + + argArr.forEach(function (argItem) { + var isNode = argItem instanceof Node; + docFrag.appendChild(isNode ? argItem : document.createTextNode(String(argItem))); + }); + + this.insertBefore(docFrag, this.firstChild); + }; + }); +})([Element.prototype, Document.prototype, DocumentFragment.prototype]);</pre> + +<h2 id="说明">说明</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('DOM WHATWG', '#dom-parentnode-prepend', 'ParentNode.prepend()')}}</td> + <td>{{Spec2('DOM WHATWG')}}</td> + <td>Initial definition.</td> + </tr> + </tbody> +</table> + +<h2 id="兼容性">兼容性</h2> + + + +<p>{{Compat("api.ParentNode.prepend")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{domxref("ParentNode")}} and {{domxref("ChildNode")}}</li> + <li>{{domxref("ParentNode.append()")}}</li> + <li>{{domxref("Node.appendChild()")}}</li> + <li>{{domxref("Node.insertBefore()")}}</li> + <li>{{domxref("ChildNode.before()")}}</li> + <li>{{domxref("NodeList")}}</li> +</ul> diff --git a/files/zh-cn/orphaned/web/guide/events/creating_and_triggering_events/index.html b/files/zh-cn/orphaned/web/guide/events/creating_and_triggering_events/index.html new file mode 100644 index 0000000000..dbc0f65c1f --- /dev/null +++ b/files/zh-cn/orphaned/web/guide/events/creating_and_triggering_events/index.html @@ -0,0 +1,137 @@ +--- +title: 创建和触发 events +slug: orphaned/Web/Guide/Events/Creating_and_triggering_events +tags: + - Advanced + - DOM + - Guide + - events +translation_of: Web/Guide/Events/Creating_and_triggering_events +original_slug: Web/Guide/Events/Creating_and_triggering_events +--- +<p>本文演示了如何创建和分派DOM事件。这些事件通常称为<strong>合成事件</strong>,而不是浏览器本身触发的事件。</p> + +<h2 id="创建自定义事件">创建自定义事件</h2> + +<p>Events 可以使用 <a href="/zh/docs/Web/API/Event"><code>Event</code></a> 构造函数创建如下:</p> + +<pre class="brush: js">var event = new Event('build'); + +// Listen for the event. +elem.addEventListener('build', function (e) { ... }, false); + +// Dispatch the event. +elem.dispatchEvent(event);</pre> + +<p>上述代码使用了 <a href="https://wiki.developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent">EventTarget.dispatchEvent()</a> 方法。</p> + +<p><span style="line-height: 1.5;">绝大多数现代浏览器中都会支持这个构造函数(Internet Explorer 例外)。 要了解更为复杂的方法,可参考下面的 </span><a href="#The_old-fashioned_way" style="line-height: 1.5;" title="#过时的方式">过时的方式</a><span style="line-height: 1.5;"> 一节。</span></p> + +<h3 id="添加自定义数据_–_CustomEvent">添加自定义数据 – CustomEvent()</h3> + +<p>要向事件对象添加更多数据,可以使用 <a href="/zh-CN/docs/Web/API/CustomEvent">CustomEvent</a> 接口,detail 属性可用于传递自定义数据。<br> + 例如,event 可以创建如下:</p> + +<pre class="brush: js">var event = new CustomEvent('build', { 'detail': elem.dataset.time });</pre> + +<p><span style="line-height: 1.5;">下面的代码允许你在事件监听器中访问更多的数据:</span></p> + +<pre class="brush: js">function eventHandler(e) { + log('The time is: ' + e.detail); +} +</pre> + +<h3 id="过时的方式">过时的方式</h3> + +<p>早期的创建事件的方法使用了受Java启发的API。下面展示了一个示例:</p> + +<pre class="brush: js">// Create the event. +var event = document.createEvent('Event'); + +// Define that the event name is 'build'. +event.initEvent('build', true, true); + +// Listen for the event. +document.addEventListener('build', function (e) { + // e.target matches document from above +}, false); + +// target can be any Element or other EventTarget. +document.dispatchEvent(event); +</pre> + +<h3 id="事件冒泡">事件冒泡</h3> + +<p>通常需要从子元素触发事件,并让祖先捕获它:</p> + +<pre class="brush: html"><form> + <textarea></textarea> +</form></pre> + +<pre class="brush: js">const form = document.querySelector('form'); +const textarea = document.querySelector('textarea'); + +// Create a new event, allow bubbling, and provide any data you want to pass to the "details" property +const eventAwesome = new CustomEvent('awesome', { + bubbles: true, + detail: { text: () => textarea.value } +}); + +// The form element listens for the custom "awesome" event and then consoles the output of the passed text() method +form.addEventListener('awesome', e => console.log(e.detail.text())); + +// As the user types, the textarea inside the form dispatches/triggers the event to fire, and uses itself as the starting point +textarea.addEventListener('input', e => e.target.dispatchEvent(eventAwesome));</pre> + +<h3 id="动态创建和派发事件">动态创建和派发事件</h3> + +<p>元素可以侦听尚未创建的事件:</p> + +<pre class="brush: html"><code><form> + <textarea></textarea> +</form></code></pre> + +<pre class="brush: js">const form = document.querySelector('form'); +const textarea = document.querySelector('textarea'); + +form.addEventListener('awesome', e => console.log(e.detail.text())); + +textarea.addEventListener('input', function() { + // Create and dispatch/trigger an event on the fly + // Note: Optionally, we've also leveraged the "function expression" (instead of the "arrow function expression") so "this" will represent the element + this.dispatchEvent(new CustomEvent('awesome', { bubbles: true, detail: { text: () => textarea.value } })) +});</pre> + + + +<h2 id="触发内置事件">触发内置事件</h2> + +<p>下面的例子演示了一个在复选框上点击(click)的模拟(就是说在程序里生成一个click事件),这个模拟点击使用了DOM方法。<a href="http://developer.mozilla.org/samples/domref/dispatchEvent.html">参见这个动态示例</a></p> + +<pre class="brush: js">function simulateClick() { + var event = new MouseEvent('click', { + 'view': window, + 'bubbles': true, + 'cancelable': true + }); + var cb = document.getElementById('checkbox'); + var cancelled = !cb.dispatchEvent(event); + if (cancelled) { + // A handler called preventDefault. + alert("cancelled"); + } else { + // None of the handlers called preventDefault. + alert("not cancelled"); + } +} +</pre> + +<h2 id="Browser_compatibility" name="Browser_compatibility">参见</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent">CustomEvent()</a></li> + <li>{{domxref("document.createEvent()")}}</li> + <li>{{domxref("Event.initEvent()")}}</li> + <li>{{domxref("EventTarget.dispatchEvent()")}}</li> + <li>{{domxref("EventTarget.addEventListener()")}}</li> +</ul> |