go to content | go to menu | skip to language menu


setAttribute, style and Internet Explorer (IE)

Data:
4 September 2008
Kategoria:
Programowanie
Author:
Cezary Tomczyk

Trying to take advantage of ever javascript functions setAttribute ( 'style', 'properties'), and you had not worked in Internet Explorer? No precisely. And this is because IE does not support it in its entirety and incomplete implemented.

However, there is a simple solution from peterbe.com:

function rzCC(s){
   // thanks http://www.ruzee.com/blog/2006/07/\
   // retrieving-css-styles-via-javascript/
   for(var exp=/-([a-z])/; 
       exp.test(s); 
       s=s.replace(exp,RegExp.$1.toUpperCase()));
   return s;
 }

 function _setStyle(element, declaration) {
   if (declaration.charAt(declaration.length-1)==';')
     declaration = declaration.slice(0, -1);
   var k, v;
   var splitted = declaration.split(';');
   for (var i=0, len=splitted.length; i<len; i++) {
      k = rzCC(splitted[i].split(':')[0]);
      v = splitted[i].split(':')[1];
      eval("element.style."+k+"='"+v+"'");

   }
 }

Now, simply give element and give it the characteristics of style:

var closer = document.createElement('a');
 //a.setAttribute('style', 'float:left; font-weight:bold');
 _setStyle(a, 'float:left; font-weight:bold');
 a.onclick = function() { ...

Next or previous post

Leave a Reply

(required)
(required)




Search