如何使用jQuery更改HTML页面
我很好奇,有没有一种方法可以使用jQuery从一个html页面切换到另一个页面,例如几秒钟后从主页切换到第1页如何使用jQuery更改HTML页面,jquery,Jquery,我很好奇,有没有一种方法可以使用jQuery从一个html页面切换到另一个页面,例如几秒钟后从主页切换到第1页 <html> <head> <title>Home Page</title> </head> <body> </div> </body> </html> <html> <head> <
<html>
<head>
<title>Home Page</title>
</head>
<body>
</div>
</body>
</html>
<html>
<head>
<title>Page1</title>
</head>
<body>
</div>
</body>
</html>
主页
第1页
这是您的代码
setTimeout(function() {
window.location.replace("http://www.I.Will.Spam.You.com/");
}, 1000); //1 sec
从代码示例中可以看出,您希望更改页面内容,而不是重定向到新的URL。如果重定向不是您想要的,那么您可以使用jQuery轻松地更改内容(尽管没有ajax的情况下不能更改整个页面)
...
...
$(函数(){
setTimeout(函数(){
$('一,'二').toggle();
$(document.attr('title','Page 1');
}, 5000);
});
您需要的可能是重定向到新页面
是的,有很多方法可以做到这一点,如果你想停留在页面上,只是改变内容,我将向你展示一些例子
使用Ajax
您可以使用ajax更改网页内容,如下所示:
function redit() {
$.ajax({
url: 'url_of/page_to/load.html', // send ajax request
success: function (date) { // if data is recieved,
$('body').html(data); // write it in the body tag..
}
})
}
setInterval(redit(), 1000); // after one second (1000ms = 1s)
您可以在这里学习:
使用负荷
通过这种方法,您只需要一个指向页面的链接,并使用该链接加载其内容(文本)
在这里学习:FYI-您所说的是“重定向”不,jquery无法做到这一点。Javascript或html可以。我很感谢您的详细解释。
success: function () {
document.location.href='/newpage/'; // jquery
}
window.location.replace('/somepage/within/thewebsite'); // js
function redit() {
$.ajax({
url: 'url_of/page_to/load.html', // send ajax request
success: function (date) { // if data is recieved,
$('body').html(data); // write it in the body tag..
}
})
}
setInterval(redit(), 1000); // after one second (1000ms = 1s)
$( "#result" ).load( "ajax/test.html" );