当前位置:网站首页>contenteditable 元素的placeholder
contenteditable 元素的placeholder
2022-07-26 20:31:00 【紫微前端】
假设我们希望给定contenteditable元素有一个占位符:
<div contenteditable></div>1.使用:empty选择器
data-placeholder来设置占位符:<div class="editable" contenteditable data-placeholder="Edit me"></div>当元素值为空时,将显示该属性:
.editable:empty:before {
content: attr(data-placeholder);
}2. 处理事件
id和data-placeholder属性添加到元素中,如下所示:<div contenteditable data-placeholder="Edit me" id="editMe"></div>当用户关注该元素时,如果它与占位符相同,我们将重置其内容。此外,当元素失去焦点时,如果用户不输入任何内容,其内容将被设置回占位符。
const ele = document.getElementById('editMe');
// Get the placeholder attribute
const placeholder = ele.getAttribute('data-placeholder');
// Set the placeholder as initial content if it's empty
ele.innerHTML === '' && (ele.innerHTML = placeholder);
ele.addEventListener('focus', function (e) {
const value = e.target.innerHTML;
value === placeholder && (e.target.innerHTML = '');
});
ele.addEventListener('blur', function (e) {
const value = e.target.innerHTML;
value === '' && (e.target.innerHTML = placeholder);
});边栏推荐
- DeepFake捏脸真假难辨,汤姆·克鲁斯比本人还像本人!
- 牛客多校-Journey-(建图distra+卡常优化)
- Cfdiv1+2-pathwalks- (tree array + linear DP)
- Introduction of JDBC
- 2022-7-26 the seventh group of abstractions and interfaces
- How to enter the specified user method body when debugging in idea?
- 关于:获取当前客户端登录的域控
- 微服务化解决文库下载业务问题实践
- 一些意想不到的bug记录
- CFdiv1+2-Pathwalks-(树状数组+线性dp)
猜你喜欢

Browser browser cache

Why does it system need observability?

Summary of common interview questions of operating system, including answers

Explain the 190 secondary compiler (decoding table) module of SMR laminated hard disk of Western data in detail

牛客刷题——Mysql系列
![[hcie security] dual computer hot standby - primary and standby backup](/img/3b/392aabd9915a3f0997cf7a72240d63.png)
[hcie security] dual computer hot standby - primary and standby backup

Line detection based on Hough transform (matlab)

Live broadcast appointment award | senior consultant xuyanfei: how does efficiency measurement help efficient and sophisticated outsourcing management

Alkbh1
![[must read new] Keya valuation analysis of University of technology, heating energy-saving products](/img/e8/c3ac4e5f11db58031cb9249a4ba0f4.jpg)
[must read new] Keya valuation analysis of University of technology, heating energy-saving products
随机推荐
Computer professional interview topic summary, general navigation
Pointpillars: fast encoders for object detection from point clouds reading notes
Serial port communication failure
After chatting with byte programmers with a monthly salary of 3W, I realized that I had been doing chores
Redis interview questions
PLSQL package
[must read new] Keya valuation analysis of University of technology, heating energy-saving products
使用 LSTM 进行多变量时间序列预测--问题汇总
idea中设置核心配置文件的模板
SPI configuration
TCP的粘包拆包问题解决方案
Web3.0 时代,基于P2PDB实现一款Dapp的技术理论
今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
Remember the idea of solving the problem of invalid bound statement xxxxx once
About: get the domain controller of the current client login
GOM and GEE lander list file encryption tutorial
Arm TZ hardware support
SprinBoot面试题
MySQL的JDBC操作及入门案例
08_ UE4 advanced_ Start end pause menu UI