当前位置:网站首页>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);
});边栏推荐
- Flutter性能优化实践 —— UI篇
- 【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
- Web3.0 时代,基于P2PDB实现一款Dapp的技术理论
- Introduction of JDBC
- JDBC connection
- 【HCIE安全】双机热备-主备备份
- 在上传之前预览图像
- 2022 open atom global open source summit agenda express | list of sub forum agenda on July 27
- [Oracle training] - deploy Ogg known as zero downtime migration
- 记一次invalid bound statement xxxxxx 问题解决思路
猜你喜欢

如何在一个项目中使用多种不同的语言?

如何借助自动化工具落地DevOps|含低代码与DevOps应用实践
HTTP cache browser cache that rabbits can understand

Devsecops, speed and security
![[HCIA security] user authentication](/img/6f/0ab6287eac36a4647dc2a0646ba969.png)
[HCIA security] user authentication

1-《PyTorch深度学习实践》-线性模型

The hardest lesson we learned from the crypto Market

golang版本管理gvm
![[HCIA security] NAT network address translation](/img/10/3b4d011963e00229d73a7435ce8c4b.png)
[HCIA security] NAT network address translation

Browser browser cache
随机推荐
Summary of common interview questions on computer network, including answers
力扣每日一题-第43天-168. Excel表列名称
Why didn't Tencent create a game like "original God"
Mobile phone \ landline call forwarding setting method
We were tossed all night by a Kong performance bug
Line detection based on Hough transform (matlab)
event.preventDefault VS return false
功能尝鲜 | 解密 Doris 复杂数据类型 ARRAY
1-《PyTorch深度学习实践》-线性模型
Today, the company came across an Alibaba P8, which was really seen as the ceiling of the foundation
QT Foundation Day 1 (1) QT, GUI (graphical user interface) development
牛客刷题——Mysql系列
idea中检索此方法中有哪些参数以便我们使用——1.类图。2.双击shift
golang版本管理gvm
Set the template of core configuration file in idea
2022开放原子全球开源峰会议程速递 | 7 月 27 日分论坛议程一览
Is it reliable, reliable and safe to open an account with a low commission for funds in Galaxy Securities
(C语言)浅识#define
Error in render: “TypeError: data.slice is not a function“
[HCIA security] NAT network address translation