html - 如何将div作为输入

标签 html css angular sass

在我的 Angular 应用程序中,我有一个自定义图像网格,当我在最后一张图像之后自定义了带有一些 fa-icon 和文本的 div,当用户单击它时想要在 pc 中显示图像选择窗口

我试过把<input #imgeSelecter type="file" accept=".png, .jpg, .jpeg" (change)="openImageSelector(imgeSelecter)"/>在我的 cutome 中制作了 div 并将其命名为 input{display:none}在 css 中,但没有任何反应。我将在这篇文章中附上图片

 <div class="upload-img-placeholder">
          <fa-icon class="textfeild-icon [icon]="['fas', 'upload']">
          </fa-icon>
            <input
              #imgeSelecter
              type="file"
              accept=".png, .jpg, .jpeg"
              (change)="openImageSelector(imgeSelecter)"
            />
            <span>
              Upload photos
            </span>

            <span class="upload-img-messege">
              You can upload # more photos
            </span>
 </div>


.upload-img-placeholder {
        input {
          //display: none;

        }
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: percentageCalculator(16);
        background-color: #e3e7eb;
        height: percentageCalculator(130);
        width: percentageCalculator(165);
        border-radius: percentageCalculator(6);
        margin-left: percentageCalculator(20);
        margin-top: percentageCalculator(27);
        color: $sub-title-dark-color;
        cursor: pointer;
        //opacity: 0.4;
        > .upload-img-messege {
          font-size: percentageCalculator(14);
          opacity: 0.7;
          margin-left: percentageCalculator(25);
          margin-right: percentageCalculator(20);
          cursor: pointer;
      }
   }

enter image description here

最佳答案

最简单的方法

组件.html

  <input type="file" id="file" (change)="uploadFile($event.target.files[0])" accept="image/*">

  <div class="container" (click)="openFileUpload()">
    <p>Upload Photos <p>
  </div>

  <p>{{file?.name}} </p>

组件.css

#file{
  display: none;
}

.container{
  width: 150px;
  height: 150px;
  background-color: grey;
  text-align: center;
  cursor: poiner;
}

组件.ts

  openFileUpload() {
    document.getElementById('file').click();
  }

  uploadFile(file) {
   console.log(file);
  }

stackblitz 示例 https://stackblitz.com/edit/angular-zdnjja?file=src%2Fapp%2Fapp.component.ts

关于html - 如何将div作为输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57621823/

相关文章:

html - 如何使用html和css在两个div之间放置箭头

node.js - 从资源中获取文件不支持 Angular 服务器端渲染

html - 发送数据到网站

html - 溢出 :scroll not showing a disabled scroll on Chrome Mobile

javascript - 使某些元素在视口(viewport)中可见

html - Angular 4 : disable button as long as form not validated

javascript - 功能策略已在本文档中禁用地理定位

html - 垂直菜单文本装饰不起作用

javascript - 新手: Need explanation of a Js function

css - 我想摆脱列表组中的额外空间