头闻号

深圳市千色变颜料有限公司

防伪技术产品|色标、色卡|其他未分类|PVC|其他通用塑料|塑料油墨

首页 > 新闻中心 > 科技常识:用CSS实现textArea中的placeholder换行功能
科技常识:用CSS实现textArea中的placeholder换行功能
发布时间:2023-02-01 10:05:04        浏览次数:4        返回列表

今天小编跟大家讲解下有关用CSS实现textarea中的placeholder换行功能 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关用CSS实现textarea中的placeholder换行功能 的相关资料,希望小伙伴们看了有所帮助。

textarea的placeholder不能换行。例如:

<textarea placeholder="第1行 \n 第2行 <br> 第3行 \A 第4行 第5行"></textarea>

这是不会起作用的 会原封不动地输出。

官方不认为这是一个bug:

The placeholder attribute represents a short hint (a word or short phrase)

For a longer hint or other advisory text, the title attribute is more appropriate.

意思就是说placeholder表示的是一个简单的提示(一个词或者一个短语) 根本不需要换行。如文本太长 那就用title。

但是实际应用中 我们有时需要换行。如何解决 很多时候我们用Javascript来解决 其实CSS也可以实现。

由于placeholder属性是可以用css操作的 所以我们可以用:after来把placeholder的内容写到CSS中 曲线救国。

CSS Code复制内容到剪贴板 textarea::-webkit-input-placeholder:after{ display:block; content:"line@\Aline#"; color:red; };

以上是webkit的代码 Firefox类也有相应的版本:

CSS Code复制内容到剪贴板 textarea::-moz-placeholder:after{ content:"line@\Aline#"; color:red; };

来源:爱蒂网