IE 中 jQuery attr() 的替代方案?

发布于 2024-11-29 16:53:59 字数 531 浏览 0 评论 0原文

好吧,如果我错了,请纠正我,但我认为 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 技术交流群。

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

发布评论

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

评论(5

旧人九事 2024-12-06 16:53:59

我一直在 IE 上使用 attrdata-* 属性,从来没有遇到过问题。 这是一个实时版本,刚刚在 IE6、IE7 和 IE9 中进行了测试。我手边没有 IE8 盒子,但我再说一次,从来没有遇到过问题。

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.

陌上青苔 2024-12-06 16:53:59

我在 IE 中使用 attr() 时没有遇到任何问题。列出的错误的描述是:

当属性为事件时,JQuery 函数.attr 在 IE 下不起作用
像 .attr("onchange","alert('Hello event onchange!')"); 。这是
因为IE不明白这一点。您可以检查属性是否为
事件,如果是 IE,则执行 eval 函数。

具体来说,它与事件有关。常规属性应该不是问题。

I haven't had a problem with attr() working in IE. The description of the bug listed is:

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.

Specifically, it has to do with events. Regular attributes shouldn't be a problem.

何止钟意 2024-12-06 16:53:59

请尝试这个:

$this.data('websrc'); 而不是 $this.attr('data-websrc');

please try this:

$this.data('websrc'); instead of $this.attr('data-websrc');

橙味迷妹 2024-12-06 16:53:59

我有类似的问题。我有一些表单,我想轻松设置该字段是否为必填字段。我的表单输入如下所示:

<输入 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.

温柔嚣张 2024-12-06 16:53:59

如果您使用大小写混合的属性名称,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.

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