HTML+js+css實現點擊圖片彈出上傳文件窗口的兩種思路

第一種:CSS實現javascript

 

<style>
<!--
 .fileInputContainer{
        height:256px;
        background:url(upfile.png);
        position:relative;
        width: 256px;
    }
    .fileInput{
        height:256px;
        overflow: hidden;
        font-size: 300px;
        position:absolute;
        right:0;
        top:0;
        opacity: 0;
        filter:alpha(opacity=0);
        cursor:pointer;
    }
-->
</style>&nbsp;
<div class="fileInputContainer"><input class="fileInput" id="" type="file" name=""></div>java

 

第二種:javascript實現函數

<div class="face">
 <p><img class="normalFace" src="./images/upload.jpg" onclick="fileSelect();"></p>
 <form id="form_face" enctype="multipart/form-data" style="width:auto;">
  <input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();" style="display:none;">
 </form>
  </div>
  <script type="text/javascript">
    function fileSelect() {
        document.getElementById("fileToUpload").click();
    }
   
    function fileSelected() {
      // 文件選擇後觸發次函數
    }
  </script>
url

相關文章
相關標籤/搜索