[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ko/docs/Web/API/Element/innerHTML [Back]  [Original]

Element.innerHTML - Web API | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Element.innerHTML

Baseline Widely available *

This feature is well established and works across many devices and browser versions. Its been available across browsers since 2015 7.

* Some parts of this feature may have varying levels of support.

Element (property) innerHTML (element) HTML XML .

: <div>, <span>, <noembed> (&), (<), (>) , innerHTML "&amp;", "&lt;" ,"&gt;" . Node.textContent .

(element) HTML (document) , insertAdjacentHTML() .

In this article

Syntax

js
const content = element.innerHTML;

element.innerHTML = htmlString;

Value

(element) HTML DOMString . Setting the value of innerHTML () , htmlString HTML , .

Exceptions

SyntaxError

HTML , innerHTML .

NoModificationAllowedError

Document HTML .

Usage notes

innerHTML (property) , HTML .

Reading the HTML contents of an element

innerHTML (user agent) HTML XML . .

js
let contents = myElement.innerHTML;

HTML .

: HTML, XML . .

Replacing the contents of an element

innerHTML (content) .

, (document) body (attribute) , .

js
document.body.innerHTML = "";

HTML , "<" HTML "&lt;" . HTML (raw text - ) . <pre> . innerHTML . , .

js
document.documentElement.innerHTML =
  "<pre>" + document.documentElement.innerHTML.replace(/</g, "&lt;") + "</pre>";

Operational details

innerHTML , ? , .

  1. HTML XML( ) , DocumentFragment DOM .
  2. <template> , <template> content (attribute) 1 DocumentFragment .
  3. , DocumentFragment .

Security considerations

innerHTML . .

js
const name = "John";
// assuming 'el' is an HTML DOM element
el.innerHTML = name; // harmless in this case

// ...

name = "<script>alert('I am John in an annoying alert!')</script>";
el.innerHTML = name; // harmless in this case

cross-site scripting , . HTML5 innerHTML <script> .

<script> , JavaScript , innerHTML . :

js
const name = "<img src='x' >";
el.innerHTML = name; // shows the alert

innerHTML . Node.textContent . HTML (raw text) .

: , innerHTML . , innerHTML addons.mozilla.org .

Example

innerHTML .

JavaScript

js
function log(msg) {
  var logElem = document.querySelector(".log");

  var time = new Date();
  var timeStr = time.toLocaleTimeString();
  logElem.innerHTML += timeStr + ": " + msg + "<br/>";
}

log("Logging mouse events inside this container...");

log() toLocaleTimeString() Date , . "log" .

MouseEvent (mousedown, click, mouseenter ) :

js
function logEvent(event) {
  var msg =
    "Event <strong>" +
    event.type +
    "</strong> at <em>" +
    event.clientX +
    ", " +
    event.clientY +
    "</em>";
  log(msg);
}

, .

js
var boxElem = document.querySelector(".box");

boxElem.addEventListener("mousedown", logEvent);
boxElem.addEventListener("mouseup", logEvent);
boxElem.addEventListener("click", logEvent);
boxElem.addEventListener("mouseenter", logEvent);
boxElem.addEventListener("mouseleave", logEvent);

HTML

HTML .

html
<div class="box">
  <div><strong>Log:</strong></div>
  <div class="log"></div>
</div>

"box" <div> . class "log" <div> .

CSS

CSS .

css
.box {
  width: 600px;
  height: 300px;
  border: 1px solid black;
  padding: 2px 4px;
  overflow-y: scroll;
  overflow-x: auto;
}

.log {
  margin-top: 8px;
  font-family: monospace;
}

Result

. .

Specification
HTML
# dom-element-innerhtml


Web Proxy Viewer  |  New URL  |  Original Page