jQuery .attr() 似乎区分大小写 [英] jQuery .attr() seems to be case sensitive

查看:31
本文介绍了jQuery .attr() 似乎区分大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有几个自定义属性的 select 元素,我用它来动态验证它并显示适当的消息.属性的名称是驼峰式的,如

I have a select element with few custom attributes that I use to validate it on the fly and display appropriate messages. Attribute's name is camel cased as in

<select validationMessage="Must select something" ... >...

问题是在早于 1.6 的 jQuery 版本中 .attr() 似乎区分大小写.更成问题的是,它不会获取原始大小写的属性.这在 Chrome 中的 Firefox 中工作相同,但在 Internet Explorer 中按预期工作(不区分大小写).有趣的是,任何外壳(原始外壳除外)都可以工作.

The problem is that in jQuery version older than 1.6 .attr() seems to be case sensitive. What's even more problematic, that it won't fetch originally cased attributes. This works the same in Firefox in Chrome, but works as expected (case insensitive as it should be) in Internet Explorer. It's also interesting that any casing (except original one) works.

这是此问题的 JSFiddle 示例.您可以在左侧更改 jQuery lib 版本并点击运行"以检查它如何与其他版本一起使用.

Here's a JSFiddle example of this issue. You can change jQuery lib version on the left and hit "Run" to check how it works with other versions.

我该如何缓解这个问题?

推荐答案

如果将 attr() 替换为 javascript 等效的 getAttribute(),似乎不是区分大小写.

If you replace attr() with the javascript equivalent getAttribute(), it seems to not be case sensitive.

唯一的缺点是你必须先get() js 对象:

The only drawback is that you have to get() the js object first :

sel.get(0).getAttribute("validationMessage")

在此处查看结果

这篇关于jQuery .attr() 似乎区分大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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