<label class="file">File</label>
<input class="none" type="file">
jQuery:
$('.file').click(function(){ $('.none').click();});
最佳答案
你可以试试这个
var _file = document.getElementsByClassName('file')[0]; //Return a NodeList
_file.onclick = function(){
document.getElementsByClassName('none')[0].click();
}
编辑
如果有多个具有相同类的元素
var _file = document.getElementsByClassName('file'); //Return a NodeList
var _none = document.getElementsByClassName('none');
for(var x = 0;x<_file.length;x++){
(function(x){ //Creating closure
_file[x].addEventListener('click',function(){
console.log(x)
document.getElementsByClassName('none')[x].click();
})
}(x))
}
关于javascript - 这段代码在纯/ native javascript 中的等价物是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37521524/