2013-07-22 88 views
2

我想點擊一個錨點,但它顯示類型錯誤..不是函數。點擊不起作用的錨點

這裏是我的代碼:

file_browser_callback: function(field_name, url, type, win) { 
    win.document.getElementById("media").onclick();  
    win.document.getElementById(field_name).value = 'window.send_to_editor'; 
}, 

爲什麼我這樣做

我使用TinyMCE的編輯器,我想用file_browser_callback

還有就是我的文檔中打開一個ThickBox的,並選擇在粗框圖像後,將其發送回調send_to_editor#media

所以我想從send_to_editor價值,並設置爲

win.document.getElementById(field_name).value 

現在我收到此錯誤:

TypeError: win.document.getElementById(...).onclick is not a function 

請不要認爲jQuery代碼,我需要純JavaScript( TinyMCE的初始化不支持的jQuery)

回答

2

當事件已綁定addEventListener,運行onclick()將無法​​正常工作(因爲它仍然會null

你可以嘗試以下方法:

var clickEvent = document.createEvent("MouseEvent"); 
clickEvent.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0, 
          false, false, false, false, 0, null); 

document.getElementById('media').dispatchEvent(clickEvent);