jQuery jquery移动点击事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5305051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery mobile tap event
提问by Anthony James
$(function() {
$('.quickNav').live('tap',function(event) {
if ($(".select_body").is(":hidden"))
{
$(".select_body").show();
}
else
{
$(".select_body").hide();
}
});
});
This works fine except for once it is visible and you tap again it doesn't go away.
这工作正常,除了一旦它可见并且您再次点击它就不会消失。
Thoughts?
想法?
回答by adardesign
$('.quickNav').live('tap',function(event) {
$(".select_body").toggle(); // toggles the visibility/display of the element.
});
this does the same as the lengthy if/else script
这与冗长的 if/else 脚本相同
See toggle method documentationin the jQuery API docs.
回答by vahanpwns
Once the element is hidden, its height and width are zero. This means when you tap the same place, you don't actually hit the element a second time.
一旦元素被隐藏,它的高度和宽度为零。这意味着当您点击同一个位置时,您实际上不会第二次点击该元素。
I would recommend setting its opacity to zero instead. Here's kind of what you could do:
我建议将其不透明度设置为零。您可以执行以下操作:
$(function() {
$('.quickNav').live('tap',function(event) {
if ($(".select_body").is(":hidden"))
{
$(".select_body").css("opacity", 1);
}
else
{
$(".select_body").css("opacity", 0);
}
});
});
and a shorter version of the same behaviour:
以及相同行为的较短版本:
$(function() {
$('.quickNav').live('tap',function(event) {
$(".select_body").css("opacity", 1 - parseInt($(".select_body").css("opacity")));
});
});
I havent actually tested this code, so I don't even know if it will run!
我还没有实际测试过这段代码,所以我什至不知道它是否会运行!
note: fadeOut() will use hide() at the end of its animation, so it doesn't really help here.
注意:fadeOut() 将在其动画结束时使用 hide(),因此它在这里并没有真正的帮助。