admin管理员组文章数量:1425701
I have a boolean variable called isTouchscreen and some on('hover', ...) events. If the isTouchscreen boolean variable equals true then I need to change on('hover') events to on('click') events. Is this possible?
Something like:
$(".smart-suggestions").on("hover", ".option", function(){
$('.img-container img').show();
//and do more stuff....
});
if(touchscreen==true){
//change onhover event to onclick event
}
I have a boolean variable called isTouchscreen and some on('hover', ...) events. If the isTouchscreen boolean variable equals true then I need to change on('hover') events to on('click') events. Is this possible?
Something like:
$(".smart-suggestions").on("hover", ".option", function(){
$('.img-container img').show();
//and do more stuff....
});
if(touchscreen==true){
//change onhover event to onclick event
}
Share
Improve this question
edited Dec 24, 2012 at 22:45
user166390
asked Dec 24, 2012 at 22:38
AnchovyLegendAnchovyLegend
12.6k41 gold badges153 silver badges240 bronze badges
2 Answers
Reset to default 7you could do it this way:
var evt = touchscreen ? 'click' : 'hover';
$(".smart-suggestions").on( evt, ".option", function(){/*.....*/});
Alternate approach if you need both events :
$(".smart-suggestions").on('click hover', ".option", function(event){
/* create whatever conditions you need*/
if( event.type=='click' && touchscreen){
doThis();
}else{
doThat();
}
});
I suggest you always bind the 'on click' event. And conditionally, if touchscreen is false, also bind on hover event.
本文标签: javascriptChanging on(39hover39) to on(39click39) for touchscreen devicesStack Overflow
版权声明:本文标题:javascript - Changing on('hover') to on('click') for touchscreen devices? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745397397a2656877.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论