在通过 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>
当使用contenteditable属性使HTML元素变为可编辑后,常规的placeholder属性将无法显示。本文提供了一个CSS方法来实现contenteditable元素的placeholder效果,通过在空内容时显示占位符文本。该方法利用了伪元素p:empty::before和attr()函数,使得在p标签为空时显示指定的placeholder内容。
9657

被折叠的 条评论
为什么被折叠?



