给可输入的p标签添加placeholder

当使用contenteditable属性使HTML元素变为可编辑后,常规的placeholder属性将无法显示。本文提供了一个CSS方法来实现contenteditable元素的placeholder效果,通过在空内容时显示占位符文本。该方法利用了伪元素p:empty::before和attr()函数,使得在p标签为空时显示指定的placeholder内容。
摘要由CSDN通过智能技术生成

在通过 contenteditable="true" 将 p 或者 div 变成可输入以后,placeholder属性是不生效的,解决方法如下:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      p {
        width: 300px;
        height: 300px;
        background-color: antiquewhite;
        /* 去除点击p标签后的原生黄色边框 */
        outline: none;
      }
      p:empty::before {
        content: attr(placeholder);
        color: #ccc;
        font-size: 16px;
      }
    </style>
  </head>
  <body>
    <p contenteditable="true" placeholder="这里是placeholder的输入提示内容"></p>
  </body>
</html>

参考:https://www.jianshu.com/p/a667fcba16be

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值