当前位置:网站首页>Placeholder of contentedable element
Placeholder of contentedable element
2022-07-26 21:27:00 【Ziwei front end】
Suppose we want to give contenteditable Element has a placeholder :
<div contenteditable></div>1. Use :empty Selectors
data-placeholder To set placeholders :<div class="editable" contenteditable data-placeholder="Edit me"></div>When the element value is empty , The attribute will be displayed :
.editable:empty:before {
content: attr(data-placeholder);
}2. Handling events
id and data-placeholder Attribute is added to the element , As shown below :<div contenteditable data-placeholder="Edit me" id="editMe"></div>When the user pays attention to this element , If it is the same as the placeholder , We will reset its contents . Besides , When an element loses focus , If the user does not enter anything , Its contents will be set back to placeholders .
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);
});边栏推荐
- 功能尝鲜 | 解密 Doris 复杂数据类型 ARRAY
- Flutter性能优化实践 —— UI篇
- (C语言)浅识#define
- 微服务化解决文库下载业务问题实践
- Browser browser cache
- LeetCode 练习——剑指 Offer II 005. 单词长度的最大乘积
- [question] browser get request with token
- MySQL的JDBC操作及入门案例
- Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
- 修改excel默认编码
猜你喜欢

【HCIA安全】NAT网络地址转换

Industrial basic IFC - extract model structure tree

What are the characteristics of low code tools? The two development tracks of low code that can be seen by discerning people!

APaaS低代码平台(一) | 把复杂留给自己,把简单留给用户

Why didn't Tencent create a game like "original God"
Monitor MySQL based on MySQL exporter

Flutter性能优化实践 —— UI篇

GOM and GEE lander list file encryption tutorial

测试用例千万不能随便,记录由一个测试用例异常引起的思考

基于Hough变换的直线检测(Matlab)
随机推荐
JVM学习----内存结构----程序计数器&虚拟机栈&本地方法栈&堆&方法区
Relevant contents about wireless communication
DeepFake捏脸真假难辨,汤姆·克鲁斯比本人还像本人!
Introduction of JDBC
和月薪3W的字节程序员聊过后,才知道自己一直在打杂...
[virtual machine data recovery] data recovery of XenServer virtual machine unavailable due to unexpected power failure
Arm TZ hardware support
(C语言)浅识#define
Daily practice ----- there is a group of students' grades. Arrange them in descending order. To add a student's grade, insert it into the grade sequence and keep the descending order
今天公司碰到一个阿里p8,算是真正见识到了基础的天花板
缩放文本以适合元素内部
js点击图片打印图像
How to configure the legendary SF lander to automatically read the list without a network
修改excel默认编码
LiveDatade的基本使用
Summary of common interview questions of operating system, including answers
2022-7-26 the seventh group of abstractions and interfaces
[HCIA security] NAT network address translation
Cfdiv1+2-pathwalks- (tree array + linear DP)
conda报错:json.decoder.JSONDecodeError: