Chrome:使用 jQuery 触发点击事件(适用于 Firefox)

发布于 2025-01-08 06:49:20 字数 1843 浏览 0 评论 0原文

这个想法是:你有一个隐藏的文件字段和一个图像。您使用该图像来显示选择文件对话框。然后我在画布中显示图像。

<input id="ytfile-select" type="hidden" value="" name="Foto[image]" />
<input style="display:none" id="file-select" accept="image/*" 
name="Foto[image]" type="file" />
<img id="upload-image" src="/images/design/upload-image.png" 
alt="upload-image-button" />        
<canvas id="canvas">  
    Sorry, your browser doesn't support the &lt;canvas&gt; element.  
</canvas>  

$('#upload-image').click(function(){
  $('#file-select').click();
});


$('#file-select').bind('change',function(){
  var fileList = this.files;  
  var img = document.createElement("img");  
  img.classList.add("obj");  
  img.src = window.URL.createObjectURL(fileList[0]);  
  var ctx = document.getElementById('canvas').getContext("2d");
  ctx.drawImage(img,0,0);
});

在 Firefox 10 上,ctx.drawImage(img,0,0); 仅当我在该行上有 Firebug 调试器和断点时才有效。如果没有断点,它就不起作用。我在另一个干净的个人资料上检查了它。

在 Chrome 上,$('#file-select').click(); 不会打开文件对话框。


编辑:这个问题已经得到解答。但是,我不知道 Firefox 的问题是什么。有什么想法吗?

我使用这些网站来创建此代码:

编辑2: 我通过这样做解决了 Firefox 的问题: 在 var fileList = this.files; 行后面,我输入了:

reader = new FileReader();
reader.onload = function (event) {
    $('#display').append('<img src ="' + event.target.result + '">');
};
reader.readAsDataURL(fileList[0]);

The idea is: you have a file field, which is hidden, and an image. You use the image to show the select file dialog. Then I display the image in the canvas.

<input id="ytfile-select" type="hidden" value="" name="Foto[image]" />
<input style="display:none" id="file-select" accept="image/*" 
name="Foto[image]" type="file" />
<img id="upload-image" src="/images/design/upload-image.png" 
alt="upload-image-button" />        
<canvas id="canvas">  
    Sorry, your browser doesn't support the <canvas> element.  
</canvas>  

$('#upload-image').click(function(){
  $('#file-select').click();
});


$('#file-select').bind('change',function(){
  var fileList = this.files;  
  var img = document.createElement("img");  
  img.classList.add("obj");  
  img.src = window.URL.createObjectURL(fileList[0]);  
  var ctx = document.getElementById('canvas').getContext("2d");
  ctx.drawImage(img,0,0);
});

On Firefox 10, ctx.drawImage(img,0,0); only works when I have the Firebug debugger nad breakpoint on that line. Without the breakpoint, it doesn't work. I checked it on another clean profile.

On Chrome, $('#file-select').click(); doesn't open the file dialog.


Edit: this question has been already answered. However, I have no idea what the issue with Firefox is. Any ideas?

I used these websites to create this code:

Edit 2:
I resolved the issue with the Firefox by doing this:
Behind the line var fileList = this.files;, I put:

reader = new FileReader();
reader.onload = function (event) {
    $('#display').append('<img src ="' + event.target.result + '">');
};
reader.readAsDataURL(fileList[0]);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时光的容颜 2025-01-15 06:49:20

这将触发 click 事件:

$('#file-select').trigger("click");

示例:

<input type="file" id="filesel" name="file" style="position:absolute;left:-999px;top:-999px" />
<a onclick="$('​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​#filesel').trigger('click');" href="#">click to open</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

JSFiddle 演示: http://jsfiddle.net/t7P3v/

更新:

事实上,如果 <代码>输入type="file" 为 display:none 它不会弹出。您仍然可以通过一些 CSS 使其对用户不可见: position:absolute;顶部:-999px;左:-999px

This will trigger the click event:

$('#file-select').trigger("click");

Example:

<input type="file" id="filesel" name="file" style="position:absolute;left:-999px;top:-999px" />
<a onclick="$('​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​#filesel').trigger('click');" href="#">click to open</a>​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

JSFiddle demo: http://jsfiddle.net/t7P3v/

Update:

Indeed, if the input type="file" is display:none it will not pop up. Still you can make it invisible to the user by some CSS: position:absolute; top:-999px; left:-999px

苏辞 2025-01-15 06:49:20

在 jsfiddle 上尝试此代码。我不记得在哪里复制了这个插件。

插件代码

jQuery.fn.file = function(chooseHandler) {
    return this.each(function() {

        var element = $(this),
            element_name = element.attr('data-name'),
            element_id = element.attr('data-id');

        // check name of radio-group
        if (element_name == undefined) {
            alert('Error: fileInput must have name!');
        }

        var holder = $('<div></div>').appendTo(element).css({
            position:'absolute',
            overflow:'hidden',
            '-moz-opacity':'0',
            filter:'alpha(opacity: 0)',
            opacity:'0',
            zoom:'1',
            width:element.outerWidth()+'px',
            height:element.outerHeight()+'px',
            'z-index':1
        });

        var _input;

        var addInput = function() {
            _input = holder.html('<input name="' + element_name + '" type="file" style="border:none; position:absolute;cursor:pointer;">').find('input');

            if (element_id != undefined) {
                _input.attr('id', element_id);
            }

            if (chooseHandler) {
                _input.change(chooseHandler);
            }
          };

        var position = function(e) {
            holder.offset(element.offset());

            if (e) {
                _input.offset({left:e.pageX-_input.outerWidth()+25, top:e.pageY-10});
            }
        };

        addInput();

        element.mouseover(position);
        element.mousemove(position);
        position();        
    });
};

使用

<div id="my-select" data-name="name-of-autogenerated-input-element" class="some custom button style"></div>


...
$("#my-select").file();

try this code on jsfiddle. I cant remember where I copy this plugin.

plugin code

jQuery.fn.file = function(chooseHandler) {
    return this.each(function() {

        var element = $(this),
            element_name = element.attr('data-name'),
            element_id = element.attr('data-id');

        // check name of radio-group
        if (element_name == undefined) {
            alert('Error: fileInput must have name!');
        }

        var holder = $('<div></div>').appendTo(element).css({
            position:'absolute',
            overflow:'hidden',
            '-moz-opacity':'0',
            filter:'alpha(opacity: 0)',
            opacity:'0',
            zoom:'1',
            width:element.outerWidth()+'px',
            height:element.outerHeight()+'px',
            'z-index':1
        });

        var _input;

        var addInput = function() {
            _input = holder.html('<input name="' + element_name + '" type="file" style="border:none; position:absolute;cursor:pointer;">').find('input');

            if (element_id != undefined) {
                _input.attr('id', element_id);
            }

            if (chooseHandler) {
                _input.change(chooseHandler);
            }
          };

        var position = function(e) {
            holder.offset(element.offset());

            if (e) {
                _input.offset({left:e.pageX-_input.outerWidth()+25, top:e.pageY-10});
            }
        };

        addInput();

        element.mouseover(position);
        element.mousemove(position);
        position();        
    });
};

usage

<div id="my-select" data-name="name-of-autogenerated-input-element" class="some custom button style"></div>


...
$("#my-select").file();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文