views:

59

answers:

2

I'm having the weird problem that after having javascript inject some dom-elements the css-rules defined for those elements are not obeyed in IE7(i.e: styling for these elements doesn't happen). (firefox and chrome work fine, others not tested)

Things I tried: - cleared the cache - no other css-rule takes precedence (no 'more-specific' styles, etc. )

the JS (in the body) (I used prototype for the injection here, but I don't think its related) (ABout the Js: some Jsonp trickery adding photos to a div based on latitude/longitude)

<script type="text/javascript">
     function ws_results(json) {
         var div = document.createElement('div');
         div.setAttribute('class', 'pano_img_cont');
         var paras = $A(json.photos);
         paras.each(function(para){
              var img = document.createElement('img');
              img.setAttribute('src', para.photo_file_url);
              div.appendChild(img);
         });
         var cc = $('panaramio_anchor');
         Element.insert(cc.up(),{top:div});
     }
  </script>
  <script src="http://www.panoramio.com/map/get_panoramas.php?order=popularity&amp;amp;set=public&amp;amp;from=0&amp;amp;to=15&amp;amp;minx=13.375&amp;amp;miny=52.4917&amp;amp;maxx=13.424999&amp;amp;maxy=52.541702&amp;amp;size=square&amp;amp;callback=ws_results" type="text/javascript"></script>

THE CSS (to be sure, added as last styles in ie.css)

.pano_img_cont{ 
    display:block;  
    float:left;
    position:relative;  
    width:100%;     
    margin-left:6px;    
    margin-top:3px; 
    padding-right:5px;  
    margin-bottom:-18px; 
    white-space:normal; 
    padding:10px;   
    background:#f00;
}

.pano_img_cont img{
    display:inline-block; 
    width:67px; 
    height:55px;
    margin:0 3px 5px 3px;
    background:#eee;
    float:left;
}

Anyone knows what's up? perhaps css doesn't do a 're-run' of css-styling after the dom is updated automatically? hmm, just guessing here..

Thanks.

A: 

Don't use setAttribute, use jQuery's addClass method:

http://api.jquery.com/addClass/

Mike Thomsen
+5  A: 

setAttribute is broken in IE7 and lower.

Use

div.className = 'pano_img_cont'

instead.


IE's implementation of setAttribute is effectively:

HTMLElement.prototype.setAttribute = function (attribute, value) {
    this[attribute] = value;
}

… which is fine so long as the attribute name and the DOM property name are the same. class, however, is a reserved keyword in JS so the property is called className instead. This breaks in IE.

If you set the property directly, it works everywhere.

David Dorward
wow that was quick.. and working, thanks
Geert-Jan