使用jQuery或纯 JavaScript 如何将用户从一页转到另一页?
当前回答
从客户的侧面调整方向:
<!DOCTYPE html>
<html>
<head>
<title>JavaScript and jQuery example to redirect a page or URL </title>
</head>
<body>
<div id="redirect">
<h2>Redirecting to another page</h2>
</div>
<script src="scripts/jquery-1.6.2.min.js"></script>
<script>
// JavaScript code to redirect a URL
window.location.replace("http://stackoverflow.com");
// window.location.replace('http://code.shouttoday.com');
// Another way to redirect page using JavaScript
// window.location.assign('http://code.shouttoday.com');
// window.location.href = 'http://code.shouttoday.com';
// document.location.href = '/relativePath';
//jQuery code to redirect a page or URL
$(document).ready(function(){
//var url = "http://code.shouttoday.com";
//$(location).attr('href',url);
// $(window).attr('location',url)
//$(location).prop('href', url)
});
</script>
</body>
</html>
其他回答
ECMAScript 6 + jQuery,85 字节
$({jQueryCode:(url)=>location.replace(url)}).attr("jQueryCode")("http://example.com")
请不要杀我,这是个玩笑,这是个玩笑这是一个笑话。
这“为问题提供了答案”, 意思是它要求找到“使用jQuery”的解决方案,
Ferrybig显然需要解释笑话(还是开玩笑,
其他的答案是使用 jQuery 的attr()
会 议 日 程 和 议 程location
或window
不必要的物体。
这个答案也滥用了它, 但以一种更荒谬的方式。 它不是用它来设定位置, 而是用它来设置位置,attr()
获取设置位置的函数。
函数被命名jQueryCode
即使它没有任何jQuery, 并且号召一个函数somethingCode
特别是当事情甚至不是语言的时候。
“85字节”是指高尔夫代码。高尔夫显然不是你在高尔夫代码之外应该做的,此外,这个答案显然不是实际的高尔夫。
基本上, 畏缩。
单页应用在同一申请路线内
window.location.pathname = '/stack';
JavaScript :
location.href = "http://stack.com";
window.location = "http://stack.com";
jj 查询 :
$(location).attr('href', "http://www.stack.com");
$(window).attr('location', "http://www.stack.com");
角形 4
import { Router } from '@angular/router';
export class NavtabComponent{
constructor(private router: Router) {
}
this.router.navigate(['bookings/taxi']);
}
您需要在代码中输入这条线 :
$(location).attr("href","http://stackoverflow.com");
如果你没有jQuery, 和JavaScript一起去:
window.location.replace("http://stackoverflow.com");
window.location.href("http://stackoverflow.com");
如果您想做更多描述的话, 这样做会有所帮助。 如果您试图生成页码数据, 这样做的方法中有一些选项。 您可以为您想要直接进入的每页创建单独的链接 。
<a href='/path-to-page?page=1' class='pager-link'>1</a>
<a href='/path-to-page?page=2' class='pager-link'>2</a>
<span class='pager-link current-page'>3</a>
...
请注意,此示例中的当前页面在代码中和与 CSS 的处理方式不同。
如果您想要通过 AJAX 更改页码数据, 则这里是 jQuery 会出现的位置。 您要做的是将点击处理器添加到与不同页面相对应的每个锁定标记中。 此点击处理器会引用一些 jQuery 代码, 该代码会通过 AJAX 去取取下一页, 并用新数据更新表格 。 下面的例子假设您有一个返回新页面数据的网络服务 。
$(document).ready( function() {
$('a.pager-link').click( function() {
var page = $(this).attr('href').split(/\?/)[1];
$.ajax({
type: 'POST',
url: '/path-to-service',
data: page,
success: function(content) {
$('#myTable').html(content); // replace
}
});
return false; // to stop link
});
});
我只是不得不更新这种荒谬的 另一种新的jQuery方法:
var url = 'http://www.fiftywaystoleaveyourlocation.com';
$(location).prop('href', url);