jquery - 使用 jQuery 查找元素的多个索引(索引)
问题描述
我正在使用一个遗留的 jQuery 项目,需要获取具有类的表的所有列的索引unsorted
。 jQuery 可以轻松获取单个索引,但我需要获取索引数组。我怎样才能做到这一点?
例如,即使unsorted
该类有多个列,这也将返回一个整数:
$('.some-table th').index($('th.unsorted'))
其他问题
虽然这个问题提到了 jQuery,但它确实与 jQuery 或 DOM 元素无关,因此对我没有帮助。
解决方案
由于 jQuery 没有该功能,它可能是
const $ths = $('.some-table th');
let indices = $.map($('th.unsorted'), function(element) {
return $ths.index(element);
});
或者您可以自己扩展该功能。
jQuery.fn.extend({
indices: function(selector) {
return $.map($(selector), function(element) {
return this.index(element);
});
},
});
let indices = $('.some-table th').indices('th.unsorted');
此外,出于性能原因,您可以自己编写搜索逻辑。
推荐阅读
- python - 错误:在 python 中使用 cx_oracle 的语法无效
- python - 过多的多处理管道()导致套接字客户端和破折号客户端之间的延迟
- kubernetes-ingress - 使用 kubernetes 入口控制器的 Kong 在命中 Kong 时总是返回“未能从 ring-balancer 获取对等点”
- spring-boot - java.lang.IllegalArgumentException : org.keycloak.adapters.springboot.KeycloakSpringBootProperties
- windows - GetADusers - 带有选定的字段
- r - 有没有办法使用 str_replace_all 替换字符串中的 '(素数)?
- r - 在 R 中的 data.table 中动态比较列
- java - 使用 Quarkus 手动登录用户
- r - 正则表达式模式在 grepl 而不是在 dir 中工作
- reactjs - 用户单击标记时如何弹出信息窗口?