有jQuery忽略属性/数据名称的大小写? [英] have jQuery ignore case in attribute/data names?

查看:86
本文介绍了有jQuery忽略属性/数据名称的大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们将HTML5的data-*属性用于某些客户端交互设置. jQuery使用这些来完成其任务.

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

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

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.

问题:

给出以下两个标签:

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

我可以将其转换为一个函数,可以将camelCase版本传递给它,也可以对其进行检查.我只是想知道jQuery中是否有更简洁的内置功能来忽略data(或attr)值的大小写.

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.

推荐答案

对于此处给出的两种版本,您都应使用

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

.data('sampleattributename')

骆驼套(.data('sampleAttributeName'))用于属性如下:

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

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

检查此jsfiddle

这篇关于有jQuery忽略属性/数据名称的大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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