简体   繁体   English

如何在不区分大小写的Java脚本中获取元素?

[英]How to get elements in java script with case insensitiveness?

I have a query. 我有一个查询。 I want to get element in java script using it's ID. 我想使用它的ID在Java脚本中获取元素。 I can use getElementById. 我可以使用getElementById。 But the problem is that, the ID could be case in sensitive. 但是问题在于,ID可能区分大小写。 ie

Suppose my ID = "Test" 假设我的ID =“测试”

then I want to get the element with IDs "Test", "test", "tEsT", "TeSt" and all other case combinations. 那么我想获取ID为“ Test”,“ test”,“ tEsT”,“ TeSt”以及所有其他大小写组合的元素。 There is a surety that there would be only one element with the ID among all of above. 可以肯定的是,上述所有元素中只有一个具有ID的元素。

Can you please help how can I get such element such that I can get the element case insensitively? 您能帮我如何获取这样的元素,以便我可以不敏感地获取大小写吗?

Thanks in advance. 提前致谢。

You could just select all elements with IDs, then use Regex to test them case insensitively: 您可以选择所有具有ID的元素,然后使用Regex对其进行不区分大小写的测试:

var result = [].filter.call(document.querySelectorAll('[id]'), function(node) {
    return node.id.match(/test/gi);
});
console.log(result);

Maybe you can narrow down your elements more than a generic [id] selector? 也许您可以比常规[id]选择器更缩小元素的范围?

jsFiddle jsFiddle

This REGEX SELECTOR FILTER is what I use to perform similar things. 我使用此REGEX SELECTOR FILTER来执行类似的操作。

HTH, Carlo HTH,卡洛

好吧,我在jQuery中找到的唯一方法是使用过滤器功能的不区分大小写的jQuery属性选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM