Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 单击div not file输入标记时打开文件选择_Javascript_Angularjs_Html - Fatal编程技术网

Javascript 单击div not file输入标记时打开文件选择

Javascript 单击div not file输入标记时打开文件选择,javascript,angularjs,html,Javascript,Angularjs,Html,我想设计一个图像上传器。要选择图像,我们需要执行以下操作: <input type="file" /> 但我不想使用常规输入,我有一个div,我希望当用户点击它时,文件选择对话框打开,然后一切都以标准方式继续 如果可能的话,我想使用Angular.js而不是jQuery,因为我的项目在Angular.js下,我对jQuery的建议是保留一个div和一个输入[type=“file”]。应该隐藏输入,并使用jQuery触发点击输入,如下所示 HTML <div id="id"

我想设计一个图像上传器。要选择图像,我们需要执行以下操作:

<input type="file" />

但我不想使用常规输入,我有一个
div
,我希望当用户点击它时,文件选择对话框打开,然后一切都以标准方式继续


如果可能的话,我想使用
Angular.js
而不是
jQuery
,因为我的项目在
Angular.js

下,我对jQuery的建议是保留一个div和一个
输入[type=“file”]
。应该隐藏输入,并使用jQuery触发点击输入,如下所示

HTML

<div id="id">Open</div>
<input id="yourinputname" type="file" name="yourinputname" style="display: none;" />

查看您不需要javascript来完成此操作,请不要查看内联样式

<div style="position: relative; border: 1px solid red; width: 50px; height: 30px; line-height: 30px; text-align: center;" > 
    Open
    <input type="file" style="opacity: 0.0; position: absolute; top: 0; left: 0; bottom: 0; right: 0; width: 100%; height:100%;" />
</div>

打开

我会使用一个隐藏的
输入
与一个
标签
元素搭配,以我想要的方式设置样式

#获取文件{
显示:无;
}
#getFileLabel{
显示:内联块;
背景:红色;
高度:50px;
宽度:100px;
}
打开文件

我的文件上传表单,带引导和fontawesome

<html>
<head>
  <link rel="stylesheet" type="text/css" href="path/to/bootstrap.css" />
  <link rel="stylesheet" type="text/css" href="path/to/fontawesome-all.css" />
</head>

<body>

  <form method="post" action="upload.php" enctype="multipart/form-data" >
    <div class="card mb-4 shadow-sm p-4">
       <h4 class="my-0 font-weight-normal">Upload File</h4>
       <div class="card-body">
         <h2 class="card-title">

           <label style="cursor: pointer;" for="file_upload">
             <span class="text-muted fa fa-upload"></span>
           </label>
           <input type="file" id="file_upload" class="d-none"/>

         </h2>
       </div>
    </div>
  </form>

</body>
</html>

上载文件
<html>
<head>
  <link rel="stylesheet" type="text/css" href="path/to/bootstrap.css" />
  <link rel="stylesheet" type="text/css" href="path/to/fontawesome-all.css" />
</head>

<body>

  <form method="post" action="upload.php" enctype="multipart/form-data" >
    <div class="card mb-4 shadow-sm p-4">
       <h4 class="my-0 font-weight-normal">Upload File</h4>
       <div class="card-body">
         <h2 class="card-title">

           <label style="cursor: pointer;" for="file_upload">
             <span class="text-muted fa fa-upload"></span>
           </label>
           <input type="file" id="file_upload" class="d-none"/>

         </h2>
       </div>
    </div>
  </form>

</body>
</html>