javascript - click() 在 chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6167527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:43:32  来源:igfitidea点击:

javascript - click() doesn`t work in chrome

javascriptjqueryhtmlclick

提问by Andrei Stanca

I have tried this, (note that I am using jQuery):

我试过这个,(注意我使用的是 jQuery):

function HandleFileButtonClick()
{
    1. //$('#filesel').click();
    2. //document.replyform.image.click();
}

HTML:

HTML:

<input type="file" id="filesel" name="image" style="display: none;"  /> 
<a href="#"><img src="<?=TF?>/img/att.png" style="height:20px;" onclick="HandleFileButtonClick();" /></a>

neither are working in Google Chrome Browser... any ideas, or a replacement for jQuery click()

两者都不适用于 Google Chrome 浏览器......任何想法,或 jQuery click() 的替代品

回答by alex

Sounds like you are hitting a security wall designed to only allow the file upload box to be triggered by the user.

听起来您遇到了旨在仅允许用户触发文件上传框的安全墙。

You could try absolutely positioning the browser's browsebutton over your link, and then setting its opacityto 0.

您可以尝试将浏览器的browse按钮绝对定位在您的链接上,然后将其设置opacity0.

回答by Tiago

I am here to help other with a similar problem. I try use .trigger('click') to start a click event into a FILE field that was if style='display:none' and discovered that Chrome diferent from Mozila Firefox and IE don′t let it work with this style. The solution is don′t use display:none and use instead of it style='width:0px;height:0px'. The result is the same, the FILE field be hidden and you can use another button to start its works even in Chrome this time.

我在这里帮助其他有类似问题的人。我尝试使用 .trigger('click') 在一个 FILE 字段中启动一个点击事件,如果 style='display:none' 并发现与 Mozila Firefox 和 IE 不同的 Chrome 不允许它使用这种样式。解决方案是不要使用 display:none 并使用 style='width:0px;height:0px' 代替它。结果是一样的,FILE 字段被隐藏了,这次即使在 Chrome 中你也可以使用另一个按钮来开始它的工作。

Best Regards peeps.

最好的问候偷看。

回答by Svetoslav Genov

Dont't use removeClass or addClass, or width:1px for the real file inputs. Just use simple CSS: visiblity: hidden; position: absolute;

不要将 removeClass 或 addClass 或 width:1px 用于实际文件输入。只需使用简单的 CSS:visiblity: hidden; position: absolute;

This will fix all your problems in this case!

在这种情况下,这将解决您的所有问题!

回答by ssapkota

What are you trying to accomplish?

你想达到什么目的?

Maybe this is what you want:

也许这就是你想要的:

function HandleFileButtonClick()
{
    ...
}

$('#filesel').click(HandleFileButtonClick);

Note:

笔记:

If you are trying to trigger mouse click event by calling click function of JQuery, you are totally out of track. This cannot be achieved.

如果你试图通过调用 JQuery 的 click 函数来触发鼠标点击事件,你就完全跑偏了。这是无法实现的。

回答by Seth Malaki

instead of using CSS fixes to hide the file field offscreen/out of sight without the use of display:none, I use the following strategy:

我没有使用 CSS 修复将文件字段隐藏在屏幕外/视线之外而不使用display:none,而是使用以下策略:

The CSS:

CSS:

.hidden {display:none}

The HTML

HTML

<input type="file" name="file-upload" id="file-upload" class="hidden" /><button>Upload</button>

in prototype:

在原型中:

$('file-upload').removeClassName('hidden').click();$('file-upload').addClassName('hidden');

in jQuery:

在 jQuery 中:

$('#file-upload').removeClass('hidden').click().addClass('hidden');

This beats wrestling with different browser styles in my opinion. Works for me!

在我看来,这胜过与不同的浏览器风格搏斗。对我有用!