为什么这个简单的例子不起作用?:
<input id="image" type="file"/>
<img src='https://cdn.elegantthemes.com/blog/wp-content/uploads/2015/02/custom-trackable-short-url-feature.png' onclick='imageLoad()'></img>
<script>
function imageLoad() {
let ev = new Event('click', {bubbles: true});
image.dispatchEvent(ev);
}
</script>
最佳答案
确实存在某些安全隐患,但根据您的目标,如果您将 Event
构造函数更改为 MouseEvent
,则在此示例中它将起作用。
...
<body>
<input id="image" type="file" />
<img id="handle" src="https://cdn.elegantthemes.com/blog/wp-content/uploads/2015/02/custom-trackable-short-url-feature.png" />
<script>
let handle = document.getElementById('handle');
handle.addEventListener('click', imageLoad, false);
function imageLoad() {
let ev = new MouseEvent('click', {bubbles: true});
image.dispatchEvent(ev);
}
</script>
...
关于javascript - 为什么输入类型 ='file' 不调度事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175572/