当前位置:网站首页>Dom实现input的焦点触发
Dom实现input的焦点触发
2022-08-02 03:24:00 【yorup】
文本框点击时,里面的默认文字隐藏,鼠标离开文本时显示文字。
onfocus 获得鼠标焦点触发
onblur 失去鼠标焦点触发
方法一,使用两个事件
代码如下:
<input type="text" name="" id="" value="请输入信息">
<script>
var input = document.querySelector('input');
input.onfocus = function(){
this.value = '';
}
input.onblur = function(){
this.value = '请输入信息';
}
</script>方法二:封装函数
代码如下:
<input type="text" value="请输入信息" onfocus="hidtxt()" onblur="showtxt()">
<script>
var input = document.querySelector('input');
function hidtxt(){
//如果不加if,那么输入其他信息失去焦点后输入框也会清空
if(input.value == '请输入信息'){
input.value = '';
}
}
function showtxt(){
if(input.value ==''){
input.value = '请输入信息';
}}
</script>效果如下:

边栏推荐
- js 取字符串中某位置某特征的值,如华为(Huawei)=>华为
- 解决5+APP真机测试无法访问后台(同局域网)
- 每天填坑,精卫填坑第二集,TX1 配置从固态启动,安装Pytorch
- debian 10 nat and routing forwarding
- npm--package.json---require
- ThunderBirde无法登录问题、pycharm调试一直收集数据、RuntimeError: CUDA error: device-side assert triggered等疑难杂症解决
- js scope and closure
- Basic usage of Monaco Editor
- display,visibility,opacity
- Amazon sellers how to improve the conversion
猜你喜欢
随机推荐
Detailed explanation of the usage of exists in mysql
js 中this指向
Phospholipid-polyethylene glycol-thiol, DSPE-PEG-Thiol, DSPE-PEG-SH, MW: 5000
L1-039 古风排版(C)
String comparison size in MySQL (date string comparison problem)
Living to detect the Adaptive Normalized Representation Learning for GeneralizableFace Anti - Spoofing reading notes
三月底啦啦
Usage of JOIN in MySQL
每日面试题 2022/7/28
网址URL
vue3 访问数据库中的数据
环形链表---------约瑟夫问题
C语言 内联函数
js __proto__、prototype、constructor的关系
简单黑马头条项目
微信小程序云开发之模糊搜索
语义分割标签即像素值的巨坑,transforms.ToTensor()的错误使用
js scope and closure
js 之 Object.defineProperty()
正则笔记(1)- 正则表达式字符匹配攻略








