简体   繁体   English

角度-CSS-自定义类型=文件输入,如何使用按钮代替标签?

[英]Angular - CSS - custom type=file input, how to use a button instead of label?

I made a custom input field of type="file" as I didn't like the default one. 我创建了一个类型为“ file”的自定义输入字段,因为我不喜欢默认字段。 To achieve this I did: 为此,我做到了:

<input
            #uploadFile
            type="file"
            id="uploadFile"
            class="hidden-input"
            accept="image/jpeg, .jpeg, image/png, .png, image/pjpeg, .jpg, .docx, .pdf"
            (change)="selectFile($event.target.files[0])"
        />

     <label [matTooltip]="Upload a file" for="uploadFile">
<mat-icon>folder_open</mat-icon>
</label>

so basically it hides the original input, and with the tag I can open the browse file window by clicking on the mat-icon. 因此基本上可以隐藏原始输入,并使用标签可以通过单击mat-icon打开浏览文件窗口。

This works well, but instead of label I wanted to use the tag <button mat-icon-button> to have a nice ripple effect on the icon, but the "for" used in the label doesn't work for <button> . 这很好用,但是我不想使用标签,而是使用标签<button mat-icon-button><button mat-icon-button>上产生了很好的波纹效果,但是标签中使用的"for"对于<button>无效。

If I wrap my mat-icon inside the label tag with the above button tag, what would happen is, the button looks how I want it, but when I click nothing happens, I suppose it's because the button is above the label , so the label doesn't get the click, I tried using z-index but wouldn't work. 如果使用上面的按钮标签将mat-icon包装在标签标签中,将会发生button看起来像我想要的那样的情况,但是当我单击没有任何反应时,我想是因为buttonlabel上方,所以label没有获得点击,我尝试使用z-index但无法正常工作。

Any ideas on how to solve this? 关于如何解决这个问题的任何想法?

Just make a button whose click fires a click event on the hidden file input 只需创建一个按钮,其单击将触发隐藏文件输入上的click事件

<button (click)="uploadFile.click()" mat-icon-button>
 <mat-icon>arrow_upward</mat-icon>
</button>

Add this link to your index.html 将此链接添加到您的index.html

<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

To your app.module add MatIconModule 在您的app.module添加MatIconModule

in Html: 在HTML中:

<input #uploadFile
            type="file"
            id="uploadFile"
            class="hidden-input"
            accept="image/jpeg, .jpeg, image/png, .png, image/pjpeg, .jpg, .docx, .pdf"
            />

<button for="uploadFile" (click)="selectFile()">
<mat-icon>folder_open</mat-icon>
</button>

In ts make onchange function: 在ts中执行onchange函数:

 files: Array<any> = [];

  selectFile() {
    const fileUpload = document.getElementById('uploadFile') as HTMLInputElement;
    fileUpload.onchange = () => {
      for (let index = 0; index < fileUpload.files.length; index++) {
        const file = fileUpload.files[index];
        this.files.push(data: file);
      }
    };
    fileUpload.click();
  }
.label{
      padding: 10px;
      color: #fff;
    }



    input[type="file"] {
        display: none;
    }

    .label-input{
      font: bold 13px Arial;
      text-decoration: none;
      background-color: #2387aa;
      color: #EEEEEE;
      padding: 4px 79px 5px 66px;
      border-top: 1px solid #CCCCCC;
      /* border-right: 1px solid #333333; */
      /* border-bottom: 1px solid #333333; */
      border-left: 1px solid #CCCCCC;
    }


 <label class="label-input"> Upload file
                    <input type="file" id="File">
                </label> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Angular 中将 label 更改为输入 CSS - Changing label for input with CSS in Angular 如何在上使用所需的验证 <input type=“file”> 在角度4 - How to use required validation on <input type=“file”> in angular 4 如何使用按钮触发<input type="file" accept="image/*" capture> - How to use a button to trigger <input type="file" accept="image/*" capture> 通过Ajax获取的页面无法使用input type =“file”标签? - The page got through Ajax cannot use the input type = “file” label? 自定义文件输入按钮,创建一个不可见的输入文件类型 inside div inside button for file input - custom file input button, creating an invisible input file type inside div inside button for file input 输入[type=&#39;file&#39;] 标签名称 - Input[type='file'] label name 如何使用链接而不是普通文件输入按钮来浏览资源管理器文件和文件夹 - How to use a link instead of the normal file input button to explorer files and folders 如何<label>使用文件名</label>更改里面<label>的</label>文本<label><input type="file" /></label> - How to change text inside <label> with the file name of <input type="file" /> 将禁用样式(css)添加到输入类型文件按钮 - Add disabled style (css) to input type file button 不使用自定义CSS或HTML的角度材质文本输入或文本区域标签大小(使用框架方法) - Angular Material Text Input or Text Area Label Size Without Using Custom CSS or HTML(Using Framework Methods)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM