<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<style>
button,input{
width: 100px;
height: 100px;
margin-top: 100px;
}
</style>
</head>
<body>
<input type="button" value="input按钮">
<button>button按钮</button>
<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
<script>
$(function(){
$("input").on("touchstart", function(){
$(this).val("input focus");
}).on("touchend", function(){
$(this).val("input blur");
});
$("button").on("touchstart", function(){
$(this).text("button focus");
}).on("touchend", function(){
$(this).text("button blur");
});
});
</script>
</body>
</html>
项目中遇到使用安卓手机点击按钮可以获得焦点和失去焦点。IOS系统的却获取不到,这是为什么呢
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你试试touch的。
三种在规范中列出并获得跨移动设备广泛实现的基本触摸事件:
jquery已经废弃了bind方法,建议使用on来代替bind,用off来代替unbind。
其次,因为你做做了一个会导致浏览器一直弹窗的操作,所以ios的禁止掉了。
你input 和button 绑定了获取焦点和失去焦点的两个事件,而且都是弹窗。
当你先获取焦点的时候,触发弹出了一个框,弹框的操作会导致元素失去焦点,所以又会触发失去焦点的事件。
alert弹窗关闭后,浏览器会归还焦点给之前的元素,所以又会触发获得焦点的事件,如此就会一直弹窗。
你可以尝试把你的代码做如下修改:先是建议将所有的bind替换为on,其次将alert替换成比如页面插入一个p并给个id,然后改变这个p中间的文字来看效果