contenteditable型的编辑框,实现placeholder

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>contenteditable</title>
<style>
.content{
width:380px;
height:100px;
border:1px solid #e1e1e1;
-webkit-user-modify:read-write-plaintext-only;
}
.content::after  {
    content: attr(placeholder);
    position: absolute;
    top: 10px;
    color: #a9a9a9;
}
</style>
</head>
<body>
<div class="content" contenteditable="true" placeholder="说点什么吧"></div>
<script src="js/jquery-2.1.4.min.js"></script>
<script>

 $('.content').on('input', function(){
          
     var text= $(this).text().trim();
     if (text=="") {
        $(this).html("");  //会显示placeholder
     } 
}); 

</script>

编辑框里删除时,会增加<br/>标签,就不会再出现placeholder里的提示内容了,所以使用js做相应处理。

猜你喜欢

转载自blog.csdn.net/dd1145322563/article/details/81503007