问题描述
<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将单击事件添加到按钮而不是更改页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!