2015-10-21 49 views
-10

當前/代碼...如何在onload事件單擊輸入(類型=文件)

function myfunction() { 
 
    alert("hello"); 
 
    document.getElementById('file').click(); 
 
}
<body onload="myfunction();"> 
 
    <input type="file" id="file" /> 
 
</body>

現在alert顯示,但文件不會被點擊onload事件但如果我們將事件更改爲onclick,則會顯示alert以及單擊文件。

+0

你的意思'onload'? –

+0

你想打開文件對話框嗎? – AtheistP3ace

+0

爲什麼你需要啓動文件瀏覽器'onload'?我在這裏聞到一些腥味...... –

回答

1

基於現代瀏覽器的安全模型,這是不可能的。在用戶與頁面進行交互之前,click函數將不起作用。我能想到的最好的事情是onfocus事件,它要求用戶做很少的事情來與頁面交互。

嘗試......

function myfunction() { 
 
    alert("hello"); 
 
    document.getElementById('file').click(); 
 
}
<body onfocus="myfunction();"> 
 
    <input type="file" id="file" /> 
 
</body>

相關問題