jQuery .click()在Safari中不起作用 [英] jQuery .click() does not work in Safari

查看:406
本文介绍了jQuery .click()在Safari中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过几个类似的帖子,其中的解决方案似乎对这些人有用,但是我无法使用它。

I have seen several similar posts with solutions that seemed to have worked for those people, but I CANNOT get this to work.

我正在使用 http://tutorialzine.com/2013/05/mini-ajax-file-upload -form / 在我的项目中。它在所有浏览器中都可以正常工作,但在Safari中,浏览按钮不会打开文件对话框。 script.js中存在以下代码(包含在插件中):

I am using http://tutorialzine.com/2013/05/mini-ajax-file-upload-form/ in my project. It works PERFECTLY, in all browsers, except in Safari the "BROWSE" button does not open a file dialog. The following code exists in script.js (which is included for the plugin to work):

$('#drop a').click(function(){
    // Simulate a click on the file input button
    // to show the file browser dialog
    $(this).parent().find('input').click();
});

.click()不会在Safari中触发。我按照 jQuery .click()在每个浏览器上都可以使用该解决方案但是Safari 并实现了

The .click() does not fire in Safari. I have tried the solution as per jQuery .click() works on every browser but Safari and implemented

 $('#drop a').click(function(){
    var a = $(this).parent().find('input');
    var evObj = document.createEvent('MouseEvents');
    evObj.initMouseEvent('click', true, true, window);
    a.dispatchEvent(evObj);
});

然后我收到错误 dispatchEvent不是函数。然后我对此做了一些研究,并尝试了jQuery.noConflict()路由,但这并没有解决错误。另外,我在主文件中使用了很多jQuery,并且我无法为整个页面激活noConflict()模式。也没有理由jQuery应该与任何东西发生冲突,因为我只使用jQuery和普通的javascript而且我没有使用像prototype或angular这样的东西。有人知道在Safari中模拟点击的另一种方法吗?

But then I get the error that dispatchEvent is not a function. I then did some research on this, and tried the jQuery.noConflict() route, but this did not resolve the error. Also, I use a LOT of jQuery in my main file and I cannot have the noConflict() mode activated for the entire page. There is also no reason that the jQuery should be conflicting with anything as I am only using jQuery and normal javascript and I am not using anything like prototype or angular. Does anybody know of another way to simulate a click in Safari?

更新:仅供参考,我添加了提醒('测试')在上面提到的函数中(单击BROWSE时触发),我在Safari中获取警报,但它没有模拟文件输入元素的点击,即:它没有打开文件对话框。

UPDATE: Just FYI, I have added an alert('test') in the mentioned function (which triggers when "BROWSE" is clicked), and I do get the alert in Safari, but it is not simulating the click of the file input element, i.e: it is not openening the file dialog.

推荐答案

原始问题中的第二部分代码结果证明是有用的,除了两件事。

The second section of code in my original question turned out to work, except for 2 things.

1)该方法不喜欢jQuery,所以代替

1) The method does not like jQuery, so instead of

var a = $(this).parent() .find('input')[0];

我为我的文件输入分配了一个ID而是调用了

I assigned an ID to my file input and instead called

var a = document.getElementById('upload_select');

2如果输入被隐藏( display:none; ),Safari会公然忽略这个,这是,所以我做了输入font-size = 1px;和opacity = 0.

2) Safari blatantly ignores this if the input is hidden (display:none;), which is was, so instead I made the input font-size = 1px; and opacity = 0.

实现这两项更改使代码正常工作。

Implementing these two changes got the code working.

这篇关于jQuery .click()在Safari中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆