How do I use jQuery to ignore case when selecting?

大兔子大兔子 提交于 2019-11-27 02:16:37

问题


I'm currently attempting to disable a link using the following jQuery selector:

$("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.

Does anyone know how to get around this? Can I change the behaviour this once to ignore case?


回答1:


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.




回答2:


jQuery was built to be extended. You can correct it or add your own type of case-insensitive selector.

Rick Strahl: Using jQuery to search Content and creating custom Selector Filters




回答3:


You may use function "is" in jQuery. It is not case-sensitive.

   $("a").each(function() {
     if ($(this).is("a[href$=/sites/abcd/sectors]")) {
       $(this).removeAttr('href');
     }
   })



回答4:


First this is NOT VALID expression since it contains \ ,

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/

so you must escape the / to \\/

so your expression will be $("a[href$=\\/sites\\/abcd\\/sectors]").removeAttr("href");



来源:https://stackoverflow.com/questions/619621/how-do-i-use-jquery-to-ignore-case-when-selecting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!