如何禁用jQuery Mobile(iOS)中Anchor的默认行为 [英] How to disable the default behavior of an Anchor in jQuery Mobile (iOS)
问题描述
我正在使用PhoneGap和jQM为iPhone和iPad构建应用程序
I am building an app for iPhone and iPad using PhoneGap and jQM
<div class="ui-block-a">
<a id="btnLink" href="#pageID" data-role="button"></a>
</div>
并且它的功能很好,但是当我在设备上运行它并按长按,我在普通浏览器中获取默认的iPhone菜单以打开或复制链接。
and it functions fine, but when I run it on the device (didn't try the simulator) and press a long press, I get the default iPhone menu for a link in a normal browser to open or copy the link.
如何禁用此默认功能app?
how can I disable this default feature in my app?
我没有成功尝试过这些:
I tried these with no success:
$("a").click(function(event) {
event.preventDefault(); // long press menu still apear
});
$("a").bind('click',function(event) {
console.log(['preventingclick',event.type]);
event.preventDefault(); // long press menu still apear
});
如果我在taphold上绑定我仍然看到长按菜单, cancel我看到控制台日志:[防止长按,taphold]
if I bind on 'taphold' I still see the menu on a long press, but after I click cancel I see the console log: ["preventing long press","taphold"]
$("a").bind('taphold', function(event) {
console.log(['preventing long press',event.type]);
event.preventDefault(); // long press menu still apear
});
如果我在taphold事件中使用委托,像这样:
if I use delegate on 'taphold' event like this:
$("a").delegate('taphold', function(event) {
console.log(['preventing long press',event.type]);
event.preventDefault();
});
将解决问题,但我不能再附加任何事件,工作之后。
will fix the problem, but I can't attach any events anymore, so non of my buttons will work after that.
$('#btnLink').bind("click", function() {
$.mobile.changePage('mypage', 'slide'); // won't fire any more because of the delegate before
});
我知道代理将适用于现在和将来的所有元素,但我想我
I know that delegate will apply on all elements now and in the future, but I think I am getting close to the answer, but not yet.
提前感谢
推荐答案
ok让它工作,
我必须结合这两个代码修正,CSS和JavaScript
I had to combine both code fixes, CSS and JavaScript
我这样做:
body { -webkit-touch-callout: none !important; }
a { -webkit-user-select: none !important; }
在我的JavaScript中:
in my JavaScript did this:
function onBodyLoad() {
$("a").bind('taphold', function(event) {
event.preventDefault();
});
}
现在所有链接都已禁用,但如果我将事件附加到任何
and now all the links are disabled, but if I attach an event to any of them it will work with no problem
全部
这篇关于如何禁用jQuery Mobile(iOS)中Anchor的默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!