Javascript IE 中 jQuery attr() 的替代方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7052182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Alternative for jQuery attr() in IE?
提问by ryanve
Ok, correct me if I'm wrong, but I take it that jQuery attr()
does NOT work in IE. (marked wontfix) That being the case, what is the best alternative? For example, this works everywhere but IE:
好的,如果我错了,请纠正我,但我认为 jQueryattr()
在 IE 中不起作用。(标记为 wontfix)既然如此,最好的选择是什么?例如,这适用于除 IE 之外的任何地方:
jQuery(document).ready(function($) {
$('.airsrc').each(function() {
var $this = $(this);
var src = $this.attr('data-websrc');
$this.attr('src', src);
});
});
Update:Whoops...I realize the issue. I actually had this inside an if
statement based on a CSS3 media query. Media queries that aren't natively supported in IE8 or lower. The attr()
definitely works!
更新:哎呀...我意识到这个问题。我实际上在if
基于 CSS3 媒体查询的语句中包含了这个。IE8 或更低版本本身不支持的媒体查询。在attr()
肯定能行!
回答by T.J. Crowder
I use attr
with data-*
attributes on IE all the time, I've never had a problem. Here's a live version, just tested in IE6, IE7, and IE9. Don't have my IE8 box handy, but again, I've never had a problem.
我一直在 IE 上使用attr
withdata-*
属性,我从来没有遇到过问题。这是一个实时版本,刚刚在 IE6、IE7 和 IE9 中进行了测试。不要把我的 IE8 盒子放在手边,但再说一次,我从来没有遇到过问题。
回答by rossipedia
I haven't had a problem with attr()
working in IE. The description of the bug listed is:
我attr()
在 IE 中工作没有问题。列出的错误的描述是:
JQuery function .attr does not works under IE, when attribute is event like .attr("onchange","alert('Hello event onchange!')"); . It is because IE does not understand this. You can check, if attribute is event, make eval function if IE.
JQuery 函数 .attr 在 IE 下不起作用,当属性是 .attr("onchange","alert('Hello event onchange!')"); . 这是因为 IE 不理解这一点。您可以检查,如果属性是事件,如果 IE 则使 eval 函数。
Specifically, it has to do with events. Regular attributes shouldn't be a problem.
具体来说,它与事件有关。常规属性应该不成问题。
回答by thecodeparadox
please try this:
请试试这个:
$this.data('websrc');
instead of $this.attr('data-websrc');
$this.data('websrc');
代替 $this.attr('data-websrc');
回答by exoboy
I had a similar problem. I had some forms that I wanted to easily set whether the field was required or not. My form input looked like this:
我有一个类似的问题。我有一些表单,我想轻松设置该字段是否为必填字段。我的表单输入如下所示:
< input id="myid" name="myname" type="text" required="true" />
< input id="myid" name="myname" type="text" required="true" />
It worked great in everything EXCEPT IE9! Doh!
它在除 IE9 之外的所有方面都很好用!哦!
The problem is that I do not want to set the new property with jQuery, and I don't want to extend the prototype for input elements.... I just want a solution that works in all browsers without a lot of extra code or branching.
问题是我不想用 jQuery 设置新属性,也不想扩展输入元素的原型......分枝。
I tried jQuery's prop() method, but again, it had to be set manually. I wanted something that would take all the DOM elements as-loaded and extract the data that way.
我尝试了 jQuery 的 prop() 方法,但同样,它必须手动设置。我想要一些可以加载所有 DOM 元素并以这种方式提取数据的东西。
I found that the jQuery attr() method worked on all browsers except IE9. After poking at it for a while, I realized that the attribute was there but reading it was being handled a bit differently on IE9.
我发现 jQuery attr() 方法适用于除 IE9 之外的所有浏览器。在戳了一会儿之后,我意识到该属性在那里,但在 IE9 上读取它的处理方式略有不同。
So, I ended up recalling the values this way:
所以,我最终以这种方式回忆了这些值:
var val = $('#elementID').attr('required') || $('#elementID')[0].getAttribute('required');
var val = $('#elementID').attr('required') || $('#elementID')[0].getAttribute('required');
It is not perfect, but it worked great and did not require me to go back and rename my attributes with "data-" or to assign them after the DOM loaded.
它并不完美,但效果很好,并且不需要我返回并使用“data-”重命名我的属性或在加载 DOM 后分配它们。
Wouldn't it be great if jQuery 1.6.x would add this alteration to the attr() and prop() methods for us!
如果 jQuery 1.6.x 将这种更改添加到我们的 attr() 和 prop() 方法中,那岂不是很棒!
If anyone knows a more elegant solution that DOES NOT REQUIRE SETTING THE ATTRIBUTE AFTER PAGE LOAD, then please let me know.
如果有人知道不需要在页面加载后设置属性的更优雅的解决方案,请告诉我。
回答by cacheflowe
It appears that attr() will fail in IE if you use a mixed-case attribute name. Be sure to use all lowercase letters for your attribute names.
如果您使用大小写混合的属性名称,则 attr() 在 IE 中似乎会失败。请确保对属性名称使用所有小写字母。