0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[JavaScript] innerText により想定外の DOM が挿入される問題のメモ(手品が如し)

Posted at

概要

  • DOM 要素に文字列を入れると、HTML タグが勝手に挿入されるケースがあり、それの忘備録としてコードを記しておきます。

再現手順

  1. <body> の中身が空の .html ページを用意。
  2. 以下のコードをブラウザのコンソールで実行。
document.body.innerText = 'aaa\nbbb\nccc'
console.log(document.body.innerHTML)
// 'aaa<br>bbb<br>ccc'

回避策

  • textContent では起きません。
document.body.textContent = 'aaa\nbbb\nccc'
console.log(document.body.innerHTML)
// `aaa\nbbb\nccc`
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?