可能重複:
How to change the href for a hyperlink using jQuery更改href的值的超鏈接
我有一個被我需要更改數據庫生成的鏈接。
我想知道我是否可以使用jQuery來查找所需的id並更改href值?
可能重複:
How to change the href for a hyperlink using jQuery更改href的值的超鏈接
我有一個被我需要更改數據庫生成的鏈接。
我想知道我是否可以使用jQuery來查找所需的id並更改href值?
肯定的:
$("#linkId").attr("href", "http://the.new.url");
是。
<script>
$(function() {
$("#id_of_link").attr("href", "/new/url");
});
</script>
如果你的HTML是這樣的:
document.getElementById("myLink").href = "http://whatever.com/yyyy";
或者,使用jQuery:
<a href="xxx" id="myLink">whatever</a>
您可以通過普通的JavaScript這樣做更改鏈接
$("#myLink").attr("href", "http://whatever.com/yyyy");
如果你想這樣做,當頁面l請使用以下代碼
$(document).ready(function() {
$("#linkId").attr("href", "http://the.new.url");
});