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?

HTML contenteditable属性

0
Posted at

😄この投稿は、codingeverybody.jpのコンテンツをもとに作成しています。

定義と使い方

contenteditable属性は、要素のコンテンツを編集できるかどうかを指定します。この属性はtrueに設定すると要素のコンテンツを編集でき、falseに設定すると編集できません。
contenteditable属性はHTML要素のグローバル属性の一つであり、ユーザーが該当要素内のテキストやコンテンツを直接編集できるようにする属性です。
この属性を使用すると、ウェブページ上で簡単なテキスト編集が可能になります。JavaScriptと組み合わせることで、インラインテキストエディターを作成することもできます。

基本例

<div contenteditable="true" style="padding: 15px; background: #eee; border-radius: 5px;">
    この内容を直接編集してみてください。
</div>

構文

<element contenteditable="true|false|plaintext-only">

属性値

  • true : 要素のコンテンツをユーザーが編集可能な状態にします。ユーザーがクリックしたりフォーカスを受けると、テキストを修正できるようになります。
  • "" : 空文字列の場合はtrueの値と同じです。
  • false : 要素のコンテンツをユーザーが編集できない状態にします。つまり、要素内のテキストやコンテンツをクリックしたり修正したりできません。
  • plaintext-only : 一般的なテキストとしてのみ編集可能です。HTMLタグやCSS、JavaScriptコードなどの書式は適用されません。

# contenteditable="true" vs. contenteditable="plaintext-only"

次の例は、contenteditable="true"とcontenteditable="plaintext-only"の違いをよく示しています。以下のコピーするテキストをコピーして、コピーしたテキストをcontenteditable="true"とcontenteditable="plaintext-only"が設定されたそれぞれの要素に貼り付けて違いを確認してみてください!

<p>contenteditable="true"</p>
<div contenteditable="true">
    コピーしたテキストを貼り付けてください!
</div>
<p>contenteditable="plaintext-only"</p>
<div contenteditable="plaintext-only">
    コピーしたテキストを貼り付けてください!
</div>

contenteditable="true"はコピーしたテキストの書式が保持されるのに対し、contenteditable="plaintext-only"はコピーしたテキストの書式が保持されません。

contenteditable属性が適用されない要素

  • <input>
  • <textarea>

参考資料

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?