HTML contenteditable属性

栏目: Html · 发布时间: 6年前

内容简介:之前一直没有留意到有为了某个使元素可编辑,你所要做的就是在html标签上设置

前言

之前一直没有留意到有 contenteditable 这个属性,今天突然看到特意记录一下它的 用法实际用途

用法

为了某个使元素可编辑,你所要做的就是在html标签上设置 "contenteditable" 属性,它几乎支持所有的HTML元素。

contenteditable 有以下几种属性:

"true"
"false"
"inherit"
<div contenteditable="true">
  This text can be edited by the user.
</div>

通过一下代码,可以观察到如果子元素没有设置 contenteditable 属性,其默认值继承自父元素(既默认为 "inherit" 属性)

<div contenteditable="true">
  <p>Edit this content to add your own quote</p>
  <p>Edit this content to add your own quote - 2</p>
</div>

可以使用css中 caret-color 属性设置文本插入光标的颜色。

实际用途

1. div模拟textarea文本域轻松实现高度自适应

2.避免处理input、textarea的内含样式

CSS user-modify

使用css中的 user-modify 属性,也可以让普通元素可以读写。

/* Keyword values */
user-modify: read-only; (默认值)
user-modify: read-write;
user-modify: write-only;
user-modify: read-write-plaintext-only; (只允许输入纯文本,但兼容性很差)

/* Global values */
user-modify: inherit;
user-modify: initial;
user-modify: unset;

举个例子:

<div class="readwrite">The user is able to change this text.</div>
.readwrite {
  -moz-user-modify: read-write;
  -webkit-user-modify: read-write;
}

相对于 contenteditable 而言, user-modify 的兼容性就没那么理想了。

HTML contenteditable属性


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Build Your Own Web Site the Right Way Using HTML & CSS

Build Your Own Web Site the Right Way Using HTML & CSS

Ian Lloyd / SitePoint / 2006-05-02 / USD 29.95

Build Your Own Website The Right Way Using HTML & CSS teaches web development from scratch, without assuming any previous knowledge of HTML, CSS or web development techniques. This book introduces you......一起来看看 《Build Your Own Web Site the Right Way Using HTML & CSS》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

随机密码生成器
随机密码生成器

多种字符组合密码

MD5 加密
MD5 加密

MD5 加密工具