jQuery 更改超链接的 href 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8110003/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 10:01:49  来源:igfitidea点击:

Change href value for a hyperlink

jquery

提问by patrick

Possible Duplicate:
How to change the href for a hyperlink using jQuery

可能的重复:
如何使用 jQuery 更改超链接的 href

I have a link that is being generated by a database that I need to change.

我有一个由我需要更改的数据库生成的链接。

I was wondering if I can use jQuery to find the required id and change the href value?

我想知道是否可以使用 jQuery 来查找所需的 id 并更改 href 值?

回答by Dave Newton

Sure:

当然:

$("#linkId").attr("href", "http://the.new.url");

回答by jfriend00

If your HTML was like this:

如果你的 HTML 是这样的:

<a href="xxx" id="myLink">whatever</a>

You can change the link by doing this in plain javascript:

您可以通过在纯 javascript 中执行此操作来更改链接:

document.getElementById("myLink").href = "http://whatever.com/yyyy";

Or, using jQuery:

或者,使用 jQuery:

$("#myLink").attr("href", "http://whatever.com/yyyy");

回答by Saurabh Santhosh

If you want to do this when the page loads, use the following code

如果要在页面加载时执行此操作,请使用以下代码

$(document).ready(function() {

$("#linkId").attr("href", "http://the.new.url");

});

回答by regality

Yes.

是的。

<script>
$(function() {
  $("#id_of_link").attr("href", "/new/url");
});
</script>