将HTML链接替换为Javascript链接

Replace HTML link with Javascript link

本文关键字:链接 Javascript 替换 HTML      更新时间:2023-09-26

我想将我的网站导航栏中的一个按钮的URL链接更改为javascript链接。

以下是我现在拥有的:

`<a href='http://www.oldlink.com' rel='nofollow' class='deals' target='_blank'>&nbsp;</a>`

我想用代替http://www.oldlink.com

<script>this is the snippet for the new link</script>

谁能教我怎么做吗?

如果您想在用户点击链接时运行Javascript,请执行:

<a href="javascript:void(0)" onclick="some_javascript_function();" ...>
<script>
function some_javascript_function() {
    // Do what you want
}
</script>

JS Fiddle

function changelink() {
    // old value
    a = document.querySelector('a').href
    alert(a)
    // now changing value
    b = document.querySelector('a').href = "www.google.com"
    // changed value
    alert(b)
}