选择时如何使用 jQuery 忽略大小写?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/619621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use jQuery to ignore case when selecting?
提问by Alex Angas
I'm currently attempting to disable a link using the following jQuery selector:
我目前正在尝试使用以下 jQuery 选择器禁用链接:
$("a[href$=/sites/abcd/sectors]").removeAttr("href");
The problem is that sometimes the href might not always be lower case on the page. When this happens the selector no longer matches.
问题是有时页面上的 href 可能并不总是小写。发生这种情况时,选择器不再匹配。
Does anyone know how to get around this? Can I change the behaviour this once to ignore case?
有谁知道如何解决这个问题?我可以更改一次行为以忽略大小写吗?
采纳答案by EndangeredMassa
I ran into this myself. I switched the logic a bit to allow me to compare it without case. It requires a little more work, but at least it works.
我自己遇到了这个。我稍微改变了逻辑,让我可以在不区分大小写的情况下进行比较。它需要更多的工作,但至少它有效。
$('a').each(function(i,n) {
var href = $(n).attr("href");
href = href.toLowerCase();
if (href.endsWith('/sites/abcd/sectors'))
$(n).removeAttr('href');
});
You would have to figure out your own endsWith
logic.
你必须弄清楚你自己的endsWith
逻辑。
回答by Josh Stodola
jQuery was built to be extended. You can correct it or add your own type of case-insensitive selector.
jQuery 是为扩展而构建的。您可以更正它或添加您自己类型的不区分大小写的选择器。
Rick Strahl: Using jQuery to search Content and creating custom Selector Filters
回答by Vadim Dobroskok
You may use function "is" in jQuery. It is not case-sensitive.
您可以在 jQuery 中使用函数“is”。它不区分大小写。
$("a").each(function() {
if ($(this).is("a[href$=/sites/abcd/sectors]")) {
$(this).removeAttr('href');
}
})
回答by amd
First this is NOT VALIDexpression since it contains \
,
首先,这不是 VALID表达式,因为它包含\
,
If you wish to use any of the meta-characters ( such as !"#$%&'()*+,./:;<=>?@[\]^``{|}~
) as
如果您希望使用任何元字符(例如!"#$%&'()*+,./:;<=>?@[\]^``{|}~
)作为
a literal part of a name, you must escape the character with two backslashes: \\
.
名称的字面部分,您必须使用两个反斜杠对字符进行转义:\\
.
Src : http://api.jquery.com/category/selectors/
源代码:http: //api.jquery.com/category/selectors/
so you must escape the /
to \\/
所以你必须躲避/
到\\/
so your expression will be $("a[href$=\\/sites\\/abcd\\/sectors]").removeAttr("href");
所以你的表情会是 $("a[href$=\\/sites\\/abcd\\/sectors]").removeAttr("href");