jQueryMobile 将单击事件添加到按钮而不是更改页面 [英] jQueryMobile add click event to a button instead of changing page
问题描述
<p><a href="index.html" data-role="button" data-icon="arrow-r" data-iconpos="right" data-theme="a" onclick="doSomething(); return false">VERIFY</a></p>
我正在使用上面的代码,它是一个带有 onClick() 设置的 jQM 按钮.调用 onclick 并执行 doSomething() 但之后,jQM 显示错误加载页面"消息.
I am using the above code which is a jQM button with the onClick() set. The onclick is called and doSomething() is executed but after that, jQM shows the "error loading page" message.
如何抑制错误?在这种情况下,我想要 jQM 按钮但不希望它更改页面.
How can I supress the error? In this case I want the jQM button but don't want it to change page.
谢谢
推荐答案
由于您使用的是 jQuery,我建议您也使用 jQuery 来连接您的事件.据说使用 e.preventDefault();
和 e.stopImmediatePropagation();
应该阻止 jQuery mobile 对 <a/执行默认操作>
.
Since you are using jQuery I would recommend to use jQuery to wire up your events as well. With that being said using e.preventDefault();
and e.stopImmediatePropagation();
should stop jQuery mobile from performing the default action on the <a/>
.
$("#verify").click(function (e) {
e.stopImmediatePropagation();
e.preventDefault();
//Do important stuff....
});
更新
使用现有标记的更好方法是简单地将 rel="external"
添加到您的 <a/>
和您的 onclick
应该正确运行.
The better way to use your existing markup would be to simply add rel="external"
to your <a/>
And your onclick
should behave correctly.
<p>
<a href="index.html" data-role="button" data-icon="arrow-r" data-iconpos="right" data-theme="a" onclick="doSomething(); return false" rel="external">VERIFY</a>
</p>
这将起作用,因为 jQuery Mobile 会将链接视为普通的 <a/>
标记并返回 false 将简单地停止默认操作.
This will work since jQuery Mobile will treat the link as a normal <a/>
tag and return false will simply stop the default action.
这篇关于jQueryMobile 将单击事件添加到按钮而不是更改页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!