Last active
June 3, 2019 02:26
-
-
Save dyun8080/81ab54d46f0a61cd9d07e6a03eff88e9 to your computer and use it in GitHub Desktop.
ios 在 body 上绑定 click 会不起作用的问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>iOS body click 事件冒泡的 bug</title> | |
</head> | |
<body> | |
<h1>点击 h1 不会冒泡到 body</h1> | |
<p>点击 p 不会冒泡到 body</p> | |
<div>点击 div 不会冒泡到 body</div> | |
<br> | |
<span>点击 span 不会冒泡到 body</span> | |
<br> | |
<br> | |
<a href="#">点击 a 之类本身就具备交互功能的元素才能冒泡到 body</a> | |
<input type="text"> | |
<select name="" id=""></select> | |
<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> | |
<script> | |
// 影响1: 给 body 注册 click 事件, 会没有效果 | |
document.body.addEventListener('click', function() { | |
alert('body'); | |
}, false); | |
// 影响2: 通过 body 代理 click 事件(实质上也是给 body 注册 click 事件) | |
$(document.body).on('click', 'h1, p, div, span', function(event) { | |
alert(event.target.tagName); | |
}); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no"> | |
<meta http-equiv="X-UA-Compatible" content="ie=edge"> | |
<title>iOS body click 事件冒泡的 bug(修复方法)</title> | |
</head> | |
<body style="cursor: pointer;"> | |
<h2>神奇的 <code>cursor: pointer;</code></h2> | |
<p>可以直接在 body 上面添加或者在每个不具备交互功能的元素上添加</p> | |
<h1>点击 h1 冒泡到 body</h1> | |
<p>点击 p 冒泡到 body</p> | |
<div>点击 div 冒泡到 body</div> | |
<br> | |
<span>点击 span 冒泡到 body</span> | |
<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> | |
<script> | |
document.body.addEventListener('click', function() { | |
alert('body'); | |
}, false); | |
$(document.body).on('click', 'h1, p, div, span', function(event) { | |
alert(event.target.tagName); | |
}); | |
</script> | |
</body> | |
</html> |
ios 上出现点击遮罩 html 的情况
影响
在点击屏幕(一般是下方的时候)会出现遮罩
这种情况很有可能是因为 window 的高度小于设备的高度,可通过 min-height: 100%
来优化
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
影响
解决方案
cursor:pointer
效果参考