您可以尝试设置上要防止默认行为的链接click
事件。
在下面的例子中,我们添加一个类special_link
为我们想被重定向到另一个页面之前做一些特殊的链接。
当我们单击“特殊”链接,我们检查,如果给定的条件之后或不决定将用户重定向到另一个页面之前。
重定向(所有的转变东西)到另一个页面(page_2
在我们的例子),是手动完成的,用下面的代码:
$.mobile.changePage("#page_2", {
transition: "slide",
});
佛有关的方法$.mobile.changePage
的更多信息,你可以检查在线文档:
http://api.jquerymobile.com/jQuery.mobile.changePage/
在下面的例子中,如果你在文本输入1
,你会不会被重定向。否则,你会改变页面。
<!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>
我希望这有助于。如果您有任何问题,请告诉我。
嘿,先谢谢你的回答!我已经能够实现同样的事情,虽然我不知道changePage,它看起来非常方便,所以非常感谢,但是...我的问题是特别是,我想不要有时更改页面,基本上模拟例如,在表单提交中返回false。 如果$ something followLink else nothing。 谢谢! – shinax
我不确定我是否正确理解了您的问题,但您可能只需在点击声明中添加“IF”条件?我修改了代码,以便看看。 – Littm
那么,我的问题正是我无法找到什么会阻止重定向。虽然我很感激你的帮助,但我在原来的帖子中解释了这一切。 – shinax