当前位置:网站首页>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);
});边栏推荐
- 测试用例千万不能随便,记录由一个测试用例异常引起的思考
- JDBC connection
- 和月薪3W的字节程序员聊过后,才知道自己一直在打杂...
- Retrieve the parameters in this method in idea for our use -- 1. Class diagram. 2. Double click shift
- TypeScript中的类型断言
- Set the template of core configuration file in idea
- Why didn't Tencent create a game like "original God"
- 一些意想不到的bug记录
- [HCIA security] bidirectional nat
- The hardest lesson we learned from the crypto Market
猜你喜欢
![[ffmpeg] add timestamp summary to video files](/img/ae/f3f24d16f5d30c276762163867546d.png)
[ffmpeg] add timestamp summary to video files

DeepFake捏脸真假难辨,汤姆·克鲁斯比本人还像本人!

PLSQL package

idea中检索此方法中有哪些参数以便我们使用——1.类图。2.双击shift

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

2022 open atom global open source summit agenda express | list of sub forum agenda on July 27

游览器——游览器游览器缓存

Registration conditions for information system project managers in the second half of 2022 (soft examination advanced)

Retrieve the parameters in this method in idea for our use -- 1. Class diagram. 2. Double click shift

GOM and GEE lander list file encryption tutorial
随机推荐
SprinBoot面试题
kubernetes之Deployment
Test cases should never be used casually, recording the thinking caused by the exception of a test case
TypeScript中的类型断言
【虚拟机数据恢复】意外断电导致XenServer虚拟机不可用的数据恢复
Devsecops, speed and security
Introduction to the billing function of wechat payment
Alkbh1
修改excel默认编码
idea中检索此方法中有哪些参数以便我们使用——1.类图。2.双击shift
Calculation formula of retained earnings rate
Line detection based on Hough transform (matlab)
Deployment of kubernetes
[must read new] Keya valuation analysis of University of technology, heating energy-saving products
LiveDatade的基本使用
idea中debug时如何进入指定的用户方法体中?
Multivariable time series prediction using LSTM -- problem summary
苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条
There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
我们被一个 kong 的性能 bug 折腾了一个通宵