What is the best way to replace the file browse button in html?

前端 未结 5 1864
闹比i
闹比i 2020-12-03 07:59

I know that it\'s possible to replace the browse button, which is generated in html, when you use input tag with type=\"file.

I\'m not sure

相关标签:
5条回答
  • 2020-12-03 08:04

    Browsers don't really like you to mess around with file inputs, but it's possible to do this. I've seen a couple of techniques, but the simplest is to absolutely position the file input over whatever you want to use as a button, and set its opacity to zero or near-zero. This means that when the user clicks on the image (or whatever you have under there) they're actually clicking on the invisible browse button.

    For example:

    <input type="file" id="fileInput">
    <img src="...">
    
    #fileInput{
        position: absolute;
        opacity: 0;
        -moz-opacity: 0;
        filter: alpha(opacity=0);
    }
    
    0 讨论(0)
  • If you don't mind using javascript you can set the opasity of the file-input to 0, place your styled control on top via z-index and send clickevents from your button to the file-input. See here for the technique: http://www.quirksmode.org/dom/inputfile.html

    0 讨论(0)
  • 2020-12-03 08:07

    The best way is to make the file input control almost invisible (by giving it a very low opacity - do not do "visibility: hidden" or "display: none") and absolutely position something under it - with a lower z-index.

    This way, the actual control will not be visible, and whatever you put under it will show through. But since the control is positioned above that button, it will still capture the click events (this is why you want to use opacity, not visibility or display - browsers make the element unclickable if you use those to hide it).

    This article goes in-depth on the technique.

    0 讨论(0)
  • 2020-12-03 08:09

    This isn't technically possible for security purposes, so the user cannot be misled.

    However, there are a couple of workarounds - take a look at http://www.quirksmode.org/dom/inputfile.html for one example.

    For the record, this question has already been asked here (where I gave the same answer).

    0 讨论(0)
  • 2020-12-03 08:15

    You can use a Flash uploader like SWFupload to do this, as well.

    0 讨论(0)
提交回复
热议问题