html - 从组件外部关注组件内部的输入
问题描述
场景如下,我有一个上传文件的组件,但它看起来真的很难看(经典input type="file"
),所以我想制作一个风格化的标签(可能是图像或其他东西),当你点击它时,你会得到相同的行为,好像您单击了文件输入。
我设法通过在upload.component
我想要使用的模板中使用我的模板来完成这项工作,但由于我需要在许多不同的地方上传文件,我不想每次都重复代码。
这工作得很好..
.image-upload > input
{
display: none;
}
<div class="image-upload">
<label for="file-input">
<img src="placeholder.jpg"/>
</label>
<input id="file-input" type="file"/>
</div>
这不..
<!-- my-other.component.html -->
<label for="file-input">
<img src="placeholder.jpg"/>
</label>
<app-upload></app-upload>
<!-- upload.component.html -->
<input id="file-input" type="file" />
那么有没有办法引用组件内“封闭”的输入?
解决方案
有同样的问题,我所做的是
CSS:
#file{
position: absolute;
top: -100vh;
}
HTML:
<input type="file" id="file" name="file">
<a onclick="select_file()" id="file_button">File</a>
jQuery JS:
function select_file(){
$('#file').click();
}
我认为您可以使用 vanilla JS 来做到这一点,但我已经在我的项目中包含了 jQuery,所以无论如何......并且带有 onclick 的元素不必是链接
推荐阅读
- flutter - ChangeNotifierProvider:可以重新创建其创建的 ChangeNotifier 吗?
- mysql - MySql - 具有特定字段计数的列表
- ruby - 为什么 Ruby setter 返回传递的值,而不是实例变量的最终值?
- git - 在 Github 提交之前:你对文件管理的管家规则是什么?
- python - 如何在张量流中沿选定轴获得等级高于 2 的张量的对角线
- python - Pandas 将列与特定值进行比较
- ios - Swift 继承(扩展类)
- node.js - 邮递员正在上传到 S3,但我的节点应用程序不是
- python - 从另一个导入的文件导入文件
- vega-lite - 相关矩阵在新版本的 Vega 中不起作用?