IE 中 jQuery attr() 的替代方案?
好吧,如果我错了,请纠正我,但我认为 jQuery attr()
在 IE 中不起作用。 (标记为不会修复)既然如此,最好的选择是什么?例如,除了 IE 之外,这在任何地方都有效:
jQuery(document).ready(function($) {
$('.airsrc').each(function() {
var $this = $(this);
var src = $this.attr('data-websrc');
$this.attr('src', src);
});
});
更新: 哎呀......我意识到了这个问题。实际上,我将其包含在基于 CSS3 媒体查询的 if
语句中。 IE8 或更低版本本身不支持媒体查询。 attr()
绝对有效!
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:
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我一直在 IE 上使用
attr
和data-*
属性,从来没有遇到过问题。 这是一个实时版本,刚刚在 IE6、IE7 和 IE9 中进行了测试。我手边没有 IE8 盒子,但我再说一次,从来没有遇到过问题。I use
attr
withdata-*
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()
时没有遇到任何问题。列出的错误的描述是:具体来说,它与事件有关。常规属性应该不是问题。
I haven't had a problem with
attr()
working in IE. The description of the bug listed is:Specifically, it has to do with events. Regular attributes shouldn't be a problem.
请尝试这个:
$this.data('websrc');
而不是$this.attr('data-websrc');
please try this:
$this.data('websrc');
instead of$this.attr('data-websrc');
我有类似的问题。我有一些表单,我想轻松设置该字段是否为必填字段。我的表单输入如下所示:
<输入 id="myid" name="myname" type="text" required="true" />
除了 IE9 之外,它在所有浏览器中都表现出色!哎哟!
问题是我不想用 jQuery 设置新属性,并且我不想扩展输入元素的原型......我只是想要一个适用于所有浏览器的解决方案,而无需大量额外的代码或分枝。
我尝试了 jQuery 的 prop() 方法,但同样,它必须手动设置。我想要一些能够加载所有 DOM 元素并以这种方式提取数据的东西。
我发现 jQuery attr() 方法适用于除 IE9 之外的所有浏览器。在研究了一段时间后,我意识到该属性就在那里,但在 IE9 上读取它的处理方式有点不同。
因此,我最终以这种方式回忆了这些值:
var val = $('#elementID').attr('required') || $('#elementID')[0].getAttribute('必需');它
并不完美,但效果很好,不需要我返回并用“data-”重命名我的属性,或者在 DOM 加载后分配它们。
如果 jQuery 1.6.x 能为我们将这种更改添加到 attr() 和 prop() 方法中,那不是很棒吗!
如果有人知道一个更优雅的解决方案,不需要在页面加载后设置属性,请告诉我。
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" />
It worked great in everything EXCEPT IE9! Doh!
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.
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.
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.
So, I ended up recalling the values this way:
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.
Wouldn't it be great if jQuery 1.6.x would add this alteration to the attr() and prop() methods for us!
If anyone knows a more elegant solution that DOES NOT REQUIRE SETTING THE ATTRIBUTE AFTER PAGE LOAD, then please let me know.
如果您使用大小写混合的属性名称,attr() 在 IE 中似乎会失败。请务必使用所有小写字母作为属性名称。
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.