Inserts the given HTML text into the element at the location.

Syntax

object.insertAdjacentHTML(sWhere, sText)

Parameters

sWhere Required. String that specifies where to insert the HTML text, using one of the following values:
beforeBegin Inserts sText immediately before the object.
afterBegin Inserts sText after the start of the object but before all other content in the object.
beforeEnd Inserts sText immediately before the end of the object but after all other content in the object.
afterEnd Inserts sText immediately after the end of the object.
sText Required. String that specifies the HTML text to insert. The string can be a combination of text and HTML tags. This must be well-formed, valid HTML or this method will fail.

Return Value

No return value.

Remarks

If the text contains HTML tags, the method parses and formats the text as it is inserted.

You cannot insert text while the document is loading. Wait for the onload event to fire before attempting to call this method.

When using the insertAdjacentHTML method to insert script, you must include the DEFER attribute in the script element.

Example

This example uses the insertAdjacentHTML method to insert script into the page.

var sHTML="<input type=button onclick=" +
"go2()" + " value='Click Me'><BR>"
var sScript='<SCRIPT DEFER>'
sScript = sScript +
'function go2(){ alert("Hello from inserted script.") }'
sScript = sScript + '</script' + '>';
ScriptDiv.insertAdjacentHTML("afterBegin",sHTML + sScript);
 

See Also

innerHTML, insertAdjacentText, outerHTML



object.insertAdjacentHTML(sWhere,   sText)  
   
  在object标签内的指定位置插入HTML代码  
  sWhere:插入位置  
  如果object为<div>  
  beforeBegin   在object标签的前端           (在<div>之前)  
  afterBegin     在object标签里的最前端   (在<div>之后)  
  beforeEnd     在object标签里的最后端     (在</div>之前)  
  afterEnd   在object标签的后面             (在</div>之后)  
  sText   要插入的HTML代码  









 

=======================================

C#

注意:还有2个方法:
oElement = object.insertAdjacentElement(sWhere, oElement)
object.insertAdjacentHTML(sWhere, sText)
用法基本一样。

相关文章:

  • 2022-01-15
  • 2022-12-23
  • 2021-11-26
  • 2021-06-05
  • 2021-12-03
  • 2021-08-05
猜你喜欢
  • 2022-12-23
  • 2021-06-09
  • 2021-08-02
  • 2022-12-23
  • 2021-05-08
  • 2022-12-23
  • 2021-12-17
相关资源
相似解决方案