Onclick使用jquery更改url [英] Onclick change url using jquery
问题描述
我正在尝试使用jquery更改网址。
I am trying to change a url using jquery.
因此,点击该按钮后,它将更改父网址,但不会重定向。
So once the button is clicked it will change the parent url but it will not redirect.
例如,如果网址是,
http://www.xyz.com 然后它将成为 http:// www .xyz.com / abc 但不会被重定向。
http://www.xyz.com then it will become http://www.xyz.com/abc but will not be redirected.
我的代码:
$('.clickme').click(function () {
window.location.hash = 'xyz';
});
是否可能?
请建议。
JSfiddle: http:// jsfiddle.net/squidraj/Tn8BW/3/
JSfiddle : http://jsfiddle.net/squidraj/Tn8BW/3/
推荐答案
Javascript可以幸运地修改浏览器的历史记录,并更改网址。此示例将在浏览器的历史记录中添加新行,您可以使用后退按钮转到该页面。
Javascript can luckilly modify browser's history, and change the url. This example will add new row to your browser's history, and you are able to use back button to go to that page.
HTML:
<a href="http://www.xyz.com/abc" id="link">abc</a>
jQuery:
$('#link').click(function() {
window.history.pushState('obj', 'newtitle', '/abc');
return false;
});
或者如果你想使用网址哈希(比如你的代码):
Or if you want to use url hashes(like in your code):
$('#link').click(function () {
window.location.hash = 'xyz';
return false;
});
这不会重定向,它会停留在页面上。
That will not redirect, it stays on the page.
这篇关于Onclick使用jquery更改url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!