选择时如何使用jQuery忽略大小写? [英] How do I use jQuery to ignore case when selecting?

查看:27
本文介绍了选择时如何使用jQuery忽略大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用以下 jQuery 选择器禁用链接:

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

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

问题是有时页面上的 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?

推荐答案

我自己也遇到了.我稍微切换了逻辑以允许我在没有大小写的情况下进行比较.它需要更多的工作,但至少它有效.

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');
});

您必须弄清楚自己的 endsWith 逻辑.

You would have to figure out your own endsWith logic.

这篇关于选择时如何使用jQuery忽略大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆