我正在開發一個應用程序英特爾XDK並希望通過javascript設置一個精靈按鈕的href
。英特爾XDK:在精靈按鈕中設置href
我已經試過:
document.getElementById('button').href
document.getElementById('button').src
setAttribute
使用jQuery ..
任何人誰可以告訴我,我應該做的設置一個精靈按鈕的href在intel XDK?
編輯:
現在,我使用這個代碼:
var arr = [{
"url": "http://www.example.com/"
}]
for(j =0; j < arr.length; j++){
document.getElementById('button').addEventListener("click", function() {
window.location.href = arr[this.id].url;
});
}
ARR的[]聲明該文件的頂部。 arr []有更多的數據,但是例如我只顯示這個。
當我嘗試訪問它在此代碼它給我一個Uncaught TypeError: Cannot read property 'url' of undefined anonymous function
我已在.addEventListener
代碼之外進行了測試,工程。
如何從此代碼中訪問arr[]
?
謝謝
編輯2:我找到了我的問題的解決方案。它與變量範圍有關。通過調用this.id
而不是j
我可以訪問arr
陣列中的正確元素。
感謝您的回覆。這個解決方案確實工作,但是我想傳遞一個數組,我想從中獲取url並將其設置爲href。但它不斷給我一個錯誤,說它無法識別數組。 document.getElementById('button')。addEventListener(「click」,function(arr){window.location.href = arr [j] .getlink; }); – RSSD
你能告訴我數組的值是多少?它在哪裏定義? –
這是一個數組,它充滿了json數據。所以數組看起來像[{「id」:「1」,「url」:「www.example.com」}] – RSSD