You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用jQuery选中父级div下无id的file类型input子元素

原生无依赖实现方案

直接使用浏览器内置的querySelector接口即可实现需求,无需引入jQuery:

// 匹配id为hey的元素下所有层级中的第一个file类型input
const fileInput = document.querySelector('#hey input[type="file"]');
console.log(fileInput);

// 如果需要和你原有jQuery代码的`>`逻辑一致,仅匹配直接子元素,使用如下写法
const directChildFileInput = document.querySelector('#hey > input[type="file"]');
console.log(directChildFileInput);

// 如果需要匹配该元素下所有符合条件的file input,使用querySelectorAll
const allFileInputs = document.querySelectorAll('#hey input[type="file"]');
console.log(allFileInputs);

原有jQuery代码匹配异常的排查方向

  • 确认页面中id为hey的元素唯一,id属性在单页面内不允许重复
  • 确认是否#hey元素下存在多个你未预期的file类型input

内容的提问来源于stack exchange,提问作者K3ns4ro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 10:45:00