开发者

Unable to remove style attribute in chrome

I'm having troubles removing the styles from my span in Chrome.开发者_StackOverflow中文版 It can have a bunch of different styles possibly applied to it so I need to just remove it whoesale. Here is an example of the problem,

jsfiddle *requires chrome

The removeAttr('style') doesn't seem to be removing the style tag. When you open the debug bar though and highlight the span, it starts working perfectly.

Does anyone know why the style is not being removed and how to remove it?


Instead of trying to remove it, what about resetting it?

$("#someElement").attr("style","");


I decided to try one thing. Changing the JQuery version to 1.5.2 and it works fine in chrome. So, it appears it is a bug in the latest version of JQuery. I suggest you just use the 1.5.2 if you can get away with it or advise Chrome users to change browsers. You can also do this:

 } else {
   $('#s').attr('style');
   $('#s').removeAttr('style');    
 }

instead of this:

 } else {
   $('#s').removeAttr('style');    
  }

for a little bug-fix/hack.

http://jsfiddle.net/pjabT/7/


.style is probably locked because of its relationship to CSS.

you can obtain the same effect just clearing it with

$('#s').attr('style','');


I'm not sure why the attribute does not get removed, is the .removeAttr() - function applicable for "style"?

Anyhow, I always approach this in a different way. Used the code of your jsFiddle, it works like this

var t = true;

setInterval(function(){
    t = !t;
    if(t){
        $('#s').css({color: 'rgb(255, 0, 0)'});
    } else {
        $('#s').attr('style', '');    
    }
}, 1000);

All I changed is basically this line

$('#s').attr('style', '');   

where I just set the value of the style attribute to ''


With the css you are setting css values for this span not attribute

so what you need to do is just change your script into this

var t = true;

setInterval(function(){
    t = !t;
    if(t){
        $('#s').attr('style', "color: rgb(255, 0, 0)");
    } else {
        $('#s').removeAttr('style');    
    }
}, 1000);


I am still working on it, but here is a quick fix:

var t = true;

setInterval(function(){
t = !t;
  if(t){
     $('#s').css('color','rgb(255, 0, 0)');
   } else {
    $('#s').css('color','rgb(0,0,0)');    
   }
}, 1000);

UPDATE: It is interesting to note this code works:

var t = true;

setInterval(function(){
t = !t;
  if(t){
    $('#s').css({color: 'rgb(255, 0, 0)'});
  } else {
     $('#s').attr('style');
     $('#s').removeAttr('style');    
 }
}, 1000);


You're all right, sort of. The problem is a Webkit with element.removeAttribute('style') which doesn't work consistently. What jQuery does now (1.6.4) which was probably fixed here: http://bugs.jquery.com/ticket/9699 is first setting the style attribute to nothing with element.setAttribute('style', '') and then removing it which seems to solve this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜