此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

contenteditable

Baseline Widely available *

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨2015年7月⁩.

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

全局属性 contenteditable 是一个枚举属性,表示元素是否可被用户编辑。如果可以,浏览器会修改元素的组件以允许编辑。

尝试一下

<blockquote contenteditable="true"> <p>Edit this content to add your own quote</p> </blockquote> <cite contenteditable="true">-- Write your own name here</cite> 
blockquote { background: #eee; border-radius: 5px; margin: 16px 0; } blockquote p { padding: 15px; } cite { margin: 16px 32px; font-weight: bold; } blockquote p::before { content: "\201C"; } blockquote p::after { content: "\201D"; } [contenteditable="true"] { caret-color: red; } 

该属性必须是下面的值之一:

  • true空字符串,表示元素是可编辑的。
  • false 表示元素不是可编辑的。
  • plaintext-only 表示元素的原始文本是可编辑的,但富文本格式会被禁用。

如果没有设置该属性的值(例如:<label contenteditable>Example Label</label>),则其值被视为空字符串。

如果没给出该属性或设置了无效的属性值,则其默认值继承自父元素:即,如果父元素可编辑,该子元素也可编辑。

注意,虽然该属性允许设定的值包括 truefalse,但该属性仍是一个枚举属性而非布尔属性。

你可以使用 CSS caret-color 属性设置用于绘制文本插入 caret 的颜色。

规范

Specification
HTML
# attr-contenteditable

浏览器兼容性

参见