javascript - 如何使图像表现得像文件输入?
问题描述
当点击默认照片时,它应该让用户从他的计算机中选择一个文件,而不是让input type = "file"
用户首先点击浏览按钮而不是选择一个文件。用户应直接单击默认照片,并应出现文件选择窗口。
<html lang = "en">
<head>
<title>
Profile Click
</title>
<style>
#file{
display: none;
}
</style>
</head>
<body>
<script>
function pro1(){
document.prolis.getElementById("image") = document.prolis.getElementById("file");
}
</script>
<form name = "prolis">
<img src = "index.jpg" id = "image" onclick = "pro1()";>
<input type = "file" id = "file">
</body>
</html>
Mozilla Firefox console shows "TypeError: document.prolis.getElementById is not a function".
我应该如何使这个功能工作?这是我的默认图像:
解决方案
要使image
行为类似于输入元素file
,您只需在单击.input
.click()
img
这应该是你的代码:
function pro1(){
document.getElementById("file").click();
}
演示:
<html lang = "en">
<head>
<title>
Profile Click
</title>
<style>
#file{
display: none;
}
</style>
</head>
<body>
<script>
function pro1(){
document.getElementById("file").click();
}
</script>
<form name = "prolis">
<img src = "index.jpg" id = "image" onclick = "pro1()";>
<input type = "file" id = "file">
</body>
</html>
笔记:
你只需要使用document.getElementById("file")
它来访问一个元素id
。
推荐阅读
- android - 特定视图是否已滚动到列表视图之外?
- android - 用户配置文件视图的动画效果
- ssms - 在 BCP 数据文件中遇到意外的 EOF
- sql - SSRS 触发器以使用附加信息更新描述
- sql - 如何从两个不可连接的表中选择前 1 个 ID?
- php - 在 Drupal 中,安装响应式菜单后,页面内容丢失
- react-native - Text 组件中的上标嵌套在 Text 组件中(React Native)
- ruby - Ruby 中路径的短文件名(8.3 文件名)与长文件名
- flutter - 颤振接触加载需要很多时间
- php - 使用 AJAX 调用从 wordpress 自定义表中选择 2 数据