How do I use jQuery to ignore case when selecting?

前端 未结 4 824
不知归路
不知归路 2020-12-09 10:45

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

$(\"a[href$=/sites/abcd/sectors]\").removeAttr(\"href\");

相关标签:
4条回答
  • 2020-12-09 10:49

    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

    0 讨论(0)
  • 2020-12-09 11:02

    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");

    0 讨论(0)
  • 2020-12-09 11:06

    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');
         }
       })
    
    0 讨论(0)
  • 2020-12-09 11:07

    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.

    0 讨论(0)
提交回复
热议问题