[英]Passing variable with JavaScript function
我試圖通過下面的GetDetail
函數傳遞變量。 我可以傳遞字符串/數字,並且可以正常工作。
但是我無法傳遞變量
detLink.onclick = new Function ("GetDetails()");
detLink.setAttribute('onclick',"javascript:GetDetails()")
detLink.setAttribute('onclick',"javascript:GetDetails("+yourVariableName+")")
當您設置屬性時,您正在使用字符串數據類型,因此必須使用變量VALUE縫制函數名稱
如果您在設置點擊處理程序時可以訪問相關變量,
detLink.onclick = function() {
GetDetails(foo);
}
如果沒有
detLink.id = uniqueId;
detLink.onclick = function() {
var foo = globalData[this.id];
GetDetails(foo);
}
// somewhere else in your code, where you know the value of the variable
globalData[detLink.id] = foo;
var val1 = "Hell world"
detLink.onclick = GetDetails( val1 );
function GetDetails( myVar ){
alert ( myVar );
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.