簡體   English   中英

如何將超鏈接設置為變量

[英]How to set a hyperlink as a variable

我有以下代碼:

<div><input id="ShopButton" type="button" onclick="window.location.href='http://www.hyperlinkcode.com/button-links.php'"</div>

正常工作,除了現在我的超鏈接將是動態的。 所以我的問題是,如何設置變量,然后將其傳遞給onclick事件? 超鏈接將存儲在名為StrUCode的變量上。

我知道我必須聲明該變量,但是如何在按鈕中使用它呢?

我跌跌撞撞地通過html / javascript,只是弄濕了我的腳。

謝謝

如果您不想像我一樣使用內聯js:

var StrUCode = "http://example.org";
document.querySelector("#ShopButton").addEventListener("click",function(){
    window.location.href=StrUCode;
},false);

使用EventTarget.addEventListener(); 偵聽元素上的事件,例如: clickmouseover等。


//You can use functions too:

var StrUCode = "http://example.org";
document.querySelector("#ShopButton").addEventListener("click",function(){
    redirectMe(StrUCode);
},false);

function redirectMe(url){
    window.location.href=url;
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM