jQuery 是否忽略属性/数据名称中的大小写?

发布于 2024-12-07 18:10:54 字数 717 浏览 0 评论 0原文

我们使用 HTML5 的 data-* 属性来设置一些客户端交互。 jQuery 使用这些来完成它的任务。

问题是传入的 HTML 可能会有所不同。显然,这是应该解决的问题,但不幸的是,我并不总是能够控制生成的 HTML。

问题:

给定这两个标签:

<a data-sampleAttributeName="example">

<a data-sampleattributename="example">

是否有一种聪明的方法将它们视为同一个?

我想出的最好的办法是这样的:

var theAttribute = ($myobject).data('sampleAttributeName');

if (($myobject).data('sampleAttributeName')){
    theAttribute = ($myobject).data('sampleAttributeName')
} else {
    theAttribute = ($myobject).data('sampleattributename')
}

我可以将其转换为一个函数,我可以将驼峰版本传递给该函数并检查两者。我只是想知道 jQuery 中是否有一个更清晰的内置功能来忽略 data (或 attr)值的大小写。

We're using HTML5's data-* attributes for some of our client side interaction set up. jQuery uses these to do its thing.

The catch is that the HTML coming in may vary. Obviously this is the problem that should be fixed but I'm not always in control of the HTML being produced, unfortunately.

The question:

Given these two tags:

<a data-sampleAttributeName="example">

<a data-sampleattributename="example">

Is there a clever way to treat them as one and the same?

The best I've come up with is something like this:

var theAttribute = ($myobject).data('sampleAttributeName');

if (($myobject).data('sampleAttributeName')){
    theAttribute = ($myobject).data('sampleAttributeName')
} else {
    theAttribute = ($myobject).data('sampleattributename')
}

I could turn that into a function that I could just pass the camelCase version to and check for both as well. I was just wondering if there was a cleaner built-in feature in jQuery to ignore the case of the data (or attr) value.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

壹場煙雨 2024-12-14 18:10:54

对于此处给出的两种变体,您应该使用驼峰式大小写 (.data('sampleAttributeName')) 获取值,

.data('sampleattributename')

适用于属性如下所示的情况:

<a  data-sample-attribute-name="something">Anchor</a>

检查 这个 jsfiddle

For both the variations given here you should get the value using

.data('sampleattributename')

The camel casing ( .data('sampleAttributeName')) is for when the attribute is like this:

<a  data-sample-attribute-name="something">Anchor</a>

Check this jsfiddle

风追烟花雨 2024-12-14 18:10:54

对于您感兴趣的每个元素,迭代 .data() 返回的对象,并通过 toLowerCase() 更新该元素的 jQuery 数据。

$('a').each(function ()
{
    var $this = $(this),
        data = $this.data(),
        kTemp;

    for (var k in data)
    {
        kTemp = k.toLowerCase();
        if (k !== kTemp)
        {
            $this.data(kTemp, data[k]).removeData(k);
        }
    }
});

For each element you're interested in, iterate over the object returned by .data() and update that element's jQuery data by toLowerCase()-ing the keys.

$('a').each(function ()
{
    var $this = $(this),
        data = $this.data(),
        kTemp;

    for (var k in data)
    {
        kTemp = k.toLowerCase();
        if (k !== kTemp)
        {
            $this.data(kTemp, data[k]).removeData(k);
        }
    }
});
挖鼻大婶 2024-12-14 18:10:54

我有很多遗留代码,它们在 html 中具有数据属性。有些属性包含破折号,有些属性包含大小写混合。为了支持 html5 数据属性的 w3c 规范,以及 jQuery 1.6 中对 $.data 的更改,我创建了一个函数来将数据属性名称字符串转换为它们的 w3c 等效项;这意味着“data-fooBar”等属性将转换为“foobar”,“data-foo-barBaz”将转换为“fooBarbaz”。我需要像这样的东西添加到我的 $.data() 调用者中,这样我就不必更新现有的 html,这可能涉及数据库更新,并且找到所有数据属性并将它们更新为符合w3c规范。该函数专门设计用于 jquery 库,并检查 jquery 版本,仅替换 jQuery 版本 1.6+ 的破折号(+驼峰式)(无论 jQuery 版本如何,所有数据属性都将转换为小写)。该函数可以轻松地转换为无需 jQuery 即可工作。

用法:

var html5data = $(this).data(w3cdatakey('foo-barBaz')); //same as $.data('fooBarbaz');

看看这个小提琴:
jsfiddle 示例

I have a lot of legacy code that has data-attributes in the html. Some attributes contain dashes, and some have mixed case. In order to support w3c spec for html5 data- attributes, and the changes to $.data brought forth in jQuery 1.6, I made a function to transform data attribute name strings to their w3c equivalent; That means that attributes like 'data-fooBar' will be transformed to 'foobar' and 'data-foo-barBaz' would be transformed to 'fooBarbaz'. I needed something like this to add to my $.data() callers so I don't have to update existing html, which could involve database updates, and it would be a nightmare to find all of the data-attributes and update them to conform to the w3c spec. This function is designed specifically to be used in jquery library, and checks the jquery version, only replacing the dashes (+ camelcase) for jQuery version 1.6+ (all data-attributes will be converted to lowercase regardless of jQuery version). The function could easily be converted to work without jQuery.

Usage:

var html5data = $(this).data(w3cdatakey('foo-barBaz')); //same as $.data('fooBarbaz');

check out this fiddle:
jsfiddle example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文