<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 显示“错误加载页面”消息。
如何抑制该错误?在本例中,我想要 jQM 按钮,但不希望它更改页面。
谢谢
最佳答案
由于您正在使用 jQuery,我建议您也使用 jQuery 来连接您的事件。话虽如此,使用 e.preventDefault();
和e.stopImmediatePropagation();
应该阻止 jQuery mobile 对 <a/>
执行默认操作.
$("#verify").click(function (e) {
e.stopImmediatePropagation();
e.preventDefault();
//Do important stuff....
});
更新
使用现有标记的更好方法是简单地添加 rel="external"
给您<a/>
还有你的onclick
应该表现得正确。
<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/>
tag 并返回 false 将停止默认操作。
关于javascript - jQueryMobile 将单击事件添加到按钮而不是更改页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5676258/