开发者

Testing if an upload field will accept "click"

开发者 https://www.devze.com 2023-02-09 13:49 出处:网络
So, I would like to be able to have people click on a link, and the an input field with a file will open.But I only want this to happen if the browser has support for it.As pointed out in this answer,

So, I would like to be able to have people click on a link, and the an input field with a file will open. But I only want this to happen if the browser has support for it. As pointed out in this answer, chrome supports this. Firefox 3.6 does not, but Firefox 4 should.

I know you can frequently test for support of features in javascript, but I'm unsure how to test for this feature.

If you'd like to see what I mean, the below code shows the feature when clicking on the link. You can also play with this on my page.

<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Upload Field Click Test</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script type="text/javascript">
        $(function() {
             var clicker = document.getElementById('clicker');
             var uploader = document.getElementById('uploader');
             clicker.addEventListener("click", function(开发者_开发问答e) {
                 uploader.click();
                 e.preventDefault();
             }, false);
        });
        </script>
    </head>
    <body>
        <form>
            <input type="file" id="uploader">
        </form>
        <a href="#" id="clicker">Should click the uploader</a>
    </body>
</html>

Things that do not work:

  1. testing !uploader.click
  2. seeing if uploader.click() throws an exception


You could use JQuery to dynamically write the HTML into the document at the appropriate place

$("#mylinkID").after('<a href=" ">Whatever</a>');`

and the link would be added after the element that contained the ID "mylinkID". If no support for JS, the link doesn't get displayed.

0

精彩评论

暂无评论...
验证码 换一张
取 消