1、input 和 textarea 的区别
input:
HTML
<input>
元素用于为基于Web的表单建立交互式控件,以便接受来自用户的数据。
<input type="range" name="range" value="2" min="0" max="100" step="10">
<input type="file" name="file" accept="image/*">
<input type="month" name="month" value="2017-11">
textarea:
HTML
<textarea>
元素表明一个多行的纯文本编辑控件.
<textarea name="textarea" rows="10" cols="50">
Write something here
</textarea>
区别:
-
<textarea>
标签是成对的,有结束标签进行闭合,标签的内容写在标签对中间;<input>
是单个标签,标签的内容经过 value
属性设置;
-
<textarea>
的值是纯文本;<input>
的值根据类型不一样而不一样;
-
<textarea>
没有type
属性;<input>
有多种type
来知足表单与用户的数据交互;
-
<textarea>
的值能够是多行的,而且有rows
和cols
来控制多行结构;<input>
的值是单行的;
2、用 div 模拟 textarea 标签
步骤:
- 给 div 添加一个HTML全局属性:
contenteditable="true"
,使 div 元素变成用户可编辑的;
- 给 div 添加样式
resize: vertical;
,使 div 能够被用户调整尺寸,注意:别忘了设置 overflow: auto;
样式,由于resize
样式不适用于overflow: visible;
的块,否则 resize
不起效哦;
- 增长一个属性:
placeholder="I am placeholder"
;
- 经过 CSS 选择器获取并显示 placeholder 的值;
直接上代码:
<div class="textarea" contenteditable="true" placeholder="This is placeholder"></div>
.textarea {
height: 200px;
width: 300px;
padding: 4px;
border: 1px solid #888;
resize: vertical;
overflow: auto;
}
.textarea:empty:before {
content: attr(placeholder);
color: #bbb;
}
效果:

欢迎交流,欢迎交朋友。