且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使照片用作& lt;输入类型=& quot;文件& gt;> ;?

更新时间:2022-11-21 08:39:20

要使 image 的行为类似于输入的 file 元素,只需调用当您单击 img 时,输入文件 .click()方法.

To make the image behave like an input file element, you can just call the input file .click() method when you click on the img.

这应该是您的代码:

function pro1(){
   document.getElementById("file").click();
}

演示:

<html lang = "en">
    <head>
        <title>
            Profile Click
        </title>
        <style>
            #file{
                display: none;
            }
        </style>
    </head>
    <body>
        <script>
            function pro1(){
                document.getElementById("file").click();
            }
        </script>
        <form name = "prolis">
        <img src = "index.jpg" id = "image" onclick = "pro1()";>
        <input type = "file" id = "file">
    </body>
</html>

注意:

您只需要使用 document.getElementById("file")通过其 id 来访问元素.

You just need to use document.getElementById("file") to access an element by its id.