diff options
Diffstat (limited to 'files/zh-cn/web/api/element/insertadjacenthtml/index.html')
| -rw-r--r-- | files/zh-cn/web/api/element/insertadjacenthtml/index.html | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/files/zh-cn/web/api/element/insertadjacenthtml/index.html b/files/zh-cn/web/api/element/insertadjacenthtml/index.html new file mode 100644 index 0000000000..e0ea9cda90 --- /dev/null +++ b/files/zh-cn/web/api/element/insertadjacenthtml/index.html @@ -0,0 +1,100 @@ +--- +title: element.insertAdjacentHTML +slug: Web/API/Element/insertAdjacentHTML +tags: + - API + - DOM + - DOM Element Methods + - HTML5 & ES6 + - Method + - insertAdjacentHTML +translation_of: Web/API/Element/insertAdjacentHTML +--- +<div>{{APIRef("DOM")}}</div> + +<p><strong><code>insertAdjacentHTML()</code></strong> 方法将指定的文本解析为 {{domxref("Element")}} 元素,并将结果节点插入到DOM树中的指定位置。它不会重新解析它正在使用的元素,因此它不会破坏元素内的现有元素。这避免了额外的序列化步骤,使其比直接使用innerHTML操作更快。</p> + +<h2 id="语法">语法</h2> + +<pre class="eval notranslate"><em>element</em>.insertAdjacentHTML(<em>position</em>, <em>text</em>); +</pre> + +<dl> + <dt><code>position</code></dt> + <dd>一个 {{domxref("DOMString")}},表示插入内容相对于元素的位置,并且必须是以下字符串之一: + <ul> + <li><code><span style="display: none;"> </span><span style="display: none;"> </span>'beforebegin'</code>:元素自身的前面。</li> + <li><code>'afterbegin'</code>:<font face="Open Sans, Arial, sans-serif">插入元素内部的第一个子节点之前</font>。</li> + <li><code>'beforeend'</code>:插入元素内部的最后一个子节点之后。</li> + <li><code>'afterend'</code>:元素自身的后面。<span style="display: none;"> </span><span style="display: none;"> </span></li> + </ul> + </dd> + <dt><code>text</code></dt> + <dd>是要被解析为HTML或XML元素,并插入到DOM树中的 {{domxref("DOMString")}}。</dd> +</dl> + +<h3 id="位置名称的可视化">位置名称的可视化</h3> + +<pre class="notranslate"><!-- <strong><code style="color: red;">beforebegin</code></strong> --> +<code><p></code> + <!-- <strong><code style="color: green;">afterbegin</code></strong> --> + foo + <!-- <strong><code style="color: blue;">beforeend</code></strong> --> +<code></p> +</code><!-- <strong><code style="color: magenta;">afterend</code></strong> --> +</pre> + +<div class="note"><strong>注意:</strong> beforebegin和afterend位置,仅在节点在树中且节点具有一个parent元素时工作。</div> + +<h2 id="Example" name="Example">示例</h2> + +<pre class="brush: js notranslate" dir="rtl">// 原为 <div id="one">one</div> +var d1 = document.getElementById('one'); +d1.insertAdjacentHTML('afterend', '<div id="two">two</div>'); + +// 此时,新结构变成: +// <div id="one">one</div><div id="two">two</div> +</pre> + +<h2 id="Specification" name="Specification">注意</h2> + +<h3 id="安全问题">安全问题</h3> + +<p>使用 <code>insertAdjacentHTML</code> 插入用户输入的HTML内容的时候,需要转义之后才能使用。</p> + +<p>如果只是为了插入文本内容(而不是HTML节点),不建议使用这个方法,建议使用<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Node/textContent" title="The Node.textContent property represents the text content of a node and its descendants."><code>node.textContent</code></a> 或者 <a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Element/insertAdjacentText" title="The insertAdjacentText() method inserts a given text node at a given position relative to the element it is invoked upon."><code>node.insertAdjacentText()</code></a>。因为这样不需要经过HTML解释器的转换,性能会好一点。</p> + +<h2 id="Specification" name="Specification" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">规范</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('DOM Parsing', '#insertadjacenthtml()', 'Element.insertAdjacentHTML()')}}</td> + <td>{{ Spec2('DOM Parsing') }}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_Compatibility" name="Browser_Compatibility" style="margin-bottom: 20px; line-height: 30px; font-size: 2.14285714285714rem;">浏览器兼容性</h2> + +<div> +<div class="hidden">此页上的兼容性表是从结构化数据生成的。如果您想贡献数据,请访问 <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> 并向我们发送一个请求。</div> + +<p>{{Compat("api.Element.insertAdjacentHTML")}}</p> +</div> + +<h2 id="相关链接">相关链接</h2> + +<ul> + <li>包括Henri Sivonen 在内的某些 <a class="external" href="http://hacks.mozilla.org/2011/11/insertadjacenthtml-enables-faster-html-snippet-injection/">hacks.mozilla.org 客座文章</a> 显示,<code>insertAdjacentHTML</code> 在某些情况下可以更快。</li> + <li>{{domxref("Element.insertAdjacentElement()")}}</li> + <li>{{domxref("Element.insertAdjacentText()")}}</li> +</ul> |
