Javascript onclick事件仅在第二次单击时触发
我有一个问题。我在网上搜索过,但是那些有同样问题的人并没有完全解释他们是如何解决这个问题的。我不知道这里发生了什么 我应该制作一个名为LoadNew的按钮,当单击它时,它显示允许用户选择一个文件 HTML JavascriptJavascript onclick事件仅在第二次单击时触发,javascript,html,css,Javascript,Html,Css,我有一个问题。我在网上搜索过,但是那些有同样问题的人并没有完全解释他们是如何解决这个问题的。我不知道这里发生了什么 我应该制作一个名为LoadNew的按钮,当单击它时,它显示允许用户选择一个文件 HTML Javascript function n() { if (document.getElementById('get_file') != null) { document.getElementById('get_file').onclick = function() {
function n() {
if (document.getElementById('get_file') != null) {
document.getElementById('get_file').onclick = function() {
document.getElementById('my_file').click();
}
//LoadGame(); //triggers this function without allowing the user to open file.
}
}
最后,我不能在函数n中放置一个函数来操作文件的内容。无论我把它放在哪里,它都会在不打开文件选择对话框的情况下触发该函数。第一次单击后,该对话框不可见,因为第一次单击会向事件处理程序附加一个匿名函数以打开我的\u文件,但它不会执行该函数 第二次单击将执行匿名函数,因为它已附加。这将触发my_文件的单击事件: 此版本触发my_文件的单击事件 这是html
<input type="button" id="get_file"
onclick="showFileUpload_2();" value="Load New Game">
<input type="file" id="my_file">
不需要此功能-
document.getElementById('get_file').onclick = function() {
.....
}
将脚本更改为
<script>
function n(){
if(document.getElementById('get_file') != null){
//document.getElementById('get_file').onclick = function() { //comment this function
document.getElementById('my_file').click()
//}
//LoadGame(); //triggers this function without allowing the user to open file.
}
}
</script>
放置一个函数来操作文件的内容是什么意思?
function showFileUpload_2(){
document.getElementById('my_file').click()
}
<input type="button" id="get_file"
onclick="showFileUpload_2();" value="Load New Game">
<input type="file" id="my_file">
document.getElementById('get_file').onclick = function() {
.....
}
<script>
function n(){
if(document.getElementById('get_file') != null){
//document.getElementById('get_file').onclick = function() { //comment this function
document.getElementById('my_file').click()
//}
//LoadGame(); //triggers this function without allowing the user to open file.
}
}
</script>