2009-09-12 56 views
2

假设它在a.html中。如何使用jQuery更改网址?

如何使用jQuery将url更改为b.html?

+1

我想他的意思是,如果你在页面a.html,你怎么使用jQuery来更改URL,以b.html - 这是一个坏榜样,因为你绝不会那样做,但你可能要在最后添加一个哈希标签或其他东西。即在触发jQuery函数时,'page.html'可能需要更改为'page.html#section'。我个人不知道该怎么做。 – Alex 2011-08-04 13:17:29

回答

7

我假设你想改变使用jQuery的<a>元素的目标(href)?

$("a#your_id").attr("href", "b.html"); // where `a#your_id` is your selector 

或者,您想更改浏览器中的当前位置(将某人重定向到另一个页面)?

window.location = "b.html"; // No jQuery required for this! 
+1

我相信位置属性是在窗口对象上。 – 2009-09-12 03:10:11

+0

糟糕 - 我的Javascript有点生疏...谢谢。 – 2009-09-12 03:13:02

1
如果你想为分配b.html

,如果你的问题是如何改变现有的href = a.html到HREF = b.html,那么这应该工作..

$('a[href=a.html]').attr('href', 'b.html'); 
0

您标记,需要使用'src'属性。

$('a#your_id').attr('src', 'b.html');