Javascript.click()在safari中未启动

Javascript .click() not firing in safari

本文关键字:启动 safari click Javascript      更新时间:2023-09-26

我的应用程序中有一个文件上传输入,我正在使用链接和Javascript打开对话框。

<a class="uploadlink" style="cursor:pointer" onclick="document.getElementById('file').click()">Open</a>
<input type="file" name="file" id="file" style="display:none" onchange="document.getElementById('txt').value = this.value"/>

我的代码适用于除Safari和Apple设备之外的所有浏览器和设备。当我点击链接并检查链接时,控制台甚至没有注册错误。有人能提出解决方案吗?

由于我在safari中有display:none,它不仅没有被渲染,而且也没有被引用。我现在更改了代码如下。

Jquery解决方案

<a class="uploadlink" style="cursor:pointer" onclick="$('#file').trigger('click');">Open</a>
<input type="file" name="file" id="file" style="opacity:0" onchange="document.getElementById('txt').value = this.value"/>

Javascript解决方案

 <a class="uploadlink" style="cursor:pointer"  onclick="document.getElementById('file').click();">Open</a>

它现在甚至在狩猎中也能工作。