您可以尝试click
在要阻止默认行为的链接上设置事件。
在下面的示例中,我们将一个类添加special_link
到我们想要在被重定向到另一个页面之前对其进行特殊处理的链接。
当我们单击“特殊”链接时,我们会检查是否遵循给定条件,然后再决定将用户重定向到另一个页面。
重定向(包含所有转换内容)到另一个页面(page_2
在我们的示例中)是手动完成的,使用以下代码:
$.mobile.changePage( "#page_2", {
transition: "slide",
});
有关该方法的更多信息$.mobile.changePage
,您可以查看在线文档:
http://api.jquerymobile.com/jQuery.mobile.changePage/
在以下示例中,如果您1
在文本输入中键入 a,您将不会被重定向。否则,您将更改页面。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<script>
$(function() {
// Setting the click event for our special link
$(".special_link").click(function() {
// IF SOME CONDITION THEN... (Here, if we typed a 1 in the text input)
if($('#my_value').val()=='1') {
// We do not change page
alert("We stay where we are");
} else {
// We change page. In this example: page 2
$.mobile.changePage( "#page_2", {
transition: "slide", // You can define the transition
// you want + other parameters
});
}
});
});
</script>
</head>
<body>
<!-- PAGE 1 -->
<div data-role="page" id="page_1">
<div data-role="content">
<h1>Page 1</h1>
<input type="text" id="my_value"/>
<!-- A SPECIAL LINK -->
<a href="" class="special_link">Special link</a>
</div>
</div>
<!-- PAGE 2 -->
<div data-role="page" id="page_2">
<div data-role="content">
<h1>Page 2</h1>
<a href="#page_1">Go back to page 1</a>
</div>
</div>
</body>
</html>
我希望这有帮助。如果您有任何问题,请告诉我。