简体   繁体   English

为什么$('#file-input')。click()在浏览器控制台中调用时不会打开文件输入对话框?

[英]Why $('#file-input').click() does not open file input dialog when called in browser console?

I found this fiddle: http://jsfiddle.net/QVAC7/ which help me to open the file input dialog. 我找到了这个小提琴: http//jsfiddle.net/QVAC7/ ,它帮助我打开文件输入对话框。 The idea is simple: 这个想法很简单:

HTML : HTML

<button id="id">Open</button>
<input id="yourinputname" type="file" name="yourinputname" style="display: none;" />

JS : JS

$('#id').on('click', function() {
    $('#yourinputname').trigger('click');
});

The code works fine, when I click the button, a dialog showed up. 代码工作正常,当我单击按钮时,出现一个对话框。

But there is a strange thing that when I enter the same command to the Chrome console, the dialog did not show up. 但有一件奇怪的事情是,当我向Chrome控制台输入相同的命令时,对话框没有显示出来。

$('#yourinputname').trigger('click');

对话没有出现

Is there any reason for this? 这有什么理由吗?

If you're using the console to try and find the jQuery object associated with $('#yourinputname') inside the JSFiddle, you won't find it because the JSFiddle's "Result" section is located inside an iFrame, which contains this code's "Result" in a secondary document outside the scope of the JSFiddle app. 如果您正在使用控制台尝试在$('#yourinputname')找到与$('#yourinputname')关联的jQuery对象,则无法找到它,因为JSFiddle的“Result”部分位于iFrame中,其中包含此代码的JSFiddle应用程序范围之外的辅助文档中的“结果”。

You won't be able to run code from the console in this manner until you take the important code from this Fiddle, insert it into a page somewhere, and run it yourself. 在您从此Fiddle中获取重要代码,将其插入某个页面并自行运行之前,您将无法以这种方式从控制台运行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM