Alternative for jQuery attr() in IE?

不羁的心 提交于 2020-01-10 01:49:25

问题


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!


回答1:


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.




回答2:


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.




回答3:


please try this:

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




回答4:


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.




回答5:


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.



来源:https://stackoverflow.com/questions/7052182/alternative-for-jquery-attr-in-ie

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!