Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript更改链接的URL?_Javascript_Html - Fatal编程技术网

如何使用JavaScript更改链接的URL?

如何使用JavaScript更改链接的URL?,javascript,html,Javascript,Html,我需要知道如何更改链接的url 例如www.sitename.com到www.anothersitename.com 如何使用javascript实现这一点 <a id="myAnchor" href="http://www.microsoft.com">Microsoft</a> <button onclick="myFunction()">Change link</button> function myFunction() { //For e

我需要知道如何更改链接的url

例如www.sitename.com到www.anothersitename.com

如何使用javascript实现这一点

<a id="myAnchor" href="http://www.microsoft.com">Microsoft</a>
<button onclick="myFunction()">Change link</button>

function myFunction() {
//For example How to change Microsoft url to w3 schools url?
//what do i put here?
}

更改链接
函数myFunction(){
//例如,如何将Microsoft url更改为w3学校url?
//我在这里放什么?
}
这将设置指向google.com的链接的
href
属性。当然,此URL不能硬编码,可以用函数参数替换

这将设置指向google.com的链接的
href
属性。当然,此URL不能硬编码,可以用函数参数替换。

函数myFunction(){document.getElementById(“#myAnchor”).href=”“}函数myFunction(){document.getElementById(“#myAnchor”).href=”“}
function changeHref() {
  document.getElementById("myAnchor").setAttribute("href", "www.google.com")
}