Onclick 使用 jquery 更改 url [英] Onclick change url using jquery

查看:29
本文介绍了Onclick 使用 jquery 更改 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 jquery 更改网址.

I am trying to change a url using jquery.

因此,一旦单击按钮,它将更改父 url,但不会重定向.

So once the button is clicked it will change the parent url but it will not redirect.

例如,如果url是,

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/

推荐答案

幸运的是,Javascript 可以修改浏览器的历史记录,并更改 url.此示例将向浏览器的历史记录添加新行,您可以使用后退按钮转到该页面.

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;
});

或者如果你想使用 url 哈希(就像在你的代码中一样):

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆