iOS存在众所周知的问题,即除非元素具有CSS cursor: pointer;
click
事件但是,我不希望所有可单击的元素都在桌面上具有此行为,因此我正在寻找一种仅在检测到iOS时添加此CSS的方法。
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)){
var elements = /* here I need help to get all elements that have click event
attached, taking into account that some of them could be added dynamically */
elements.css('cursor', 'pointer');
}
不知道这是否可以,但我真的想找到一种不必担心iOS点击的好方法。
更新:这个想法就是这样:
$(document).ready(function(){
$('body').on('click', 'div.hasClick', function(){
// do something
});
/* much more similar click event attached to many elements*/
if (navigator.userAgent.match(/(iPod|iPhone|iPad)/)){
var elements = /* here I need help to get all elements that have click event
attached, taking into account that some of them could be added dynamically
*/
elements.css('cursor', 'pointer');
}
});
你是说这个吗?
$('*[onclick]').css("cursor","pointer");
div {
margin: 10px;
background: blue;
width: 20px;
height: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div onclick="javascript:void(0)"></div>
<div onclick="javascript:void(0)"></div>
<div id="onclk"></div>
搜索整个DOM以查看单击事件的事物将是昂贵的。假设您知道要检查单击事件的子集,则可以做这样的事情...
let elements = Array.from(document.querySelectorAll("button"));
let elementsWithEvent = [];
elementsWithEvent = elements.filter((element)=>{
return $(element).data('events').click ? true : false;
});