2012-11-22 26 views
1

我有一个索引根据用户选择的内容更改其模板。一旦用户更改其模板,我想让菜单也改变颜色。 它可以在所有的浏览器,但在IE它给我的错误“1”为空或不在行return "#" + hex(bg[1]) + hex(bg[2]) + hex(bg[3]);jquery - .css not working给出空值

这里的对象是我的代码

jQuery(document).ready(function(){ 

     var bg = convert(jQuery('#body').css('background-color')); 
     var font = convert(jQuery('.left').css('color')); 
     if(bg =='#ffffff' || bg=='undefined'){ 
      bg = '#000000'; 
      font='#ffffff'; 
     } 


     jQuery('.mainmenu').hover(
      function(){ 
       var $this = $(this); 
       $this.data('bgcolor', $this.css('background-color')).css('background-color', '#FFFFFF'); 
      }, 
      function(){ 
       var $this = $(this); 
       $this.css('background-color', $this.data('bgcolor')); 
      } 
     ); 
     jQuery('.submenu').hover(
      function(){ 
       var $this = $(this); 
       $this.data('bgcolor2', $this.css('background-color')).css('background-color', bg); 
      }, 
      function(){ 
       var $this = $(this); 
       $this.css('background-color', $this.data('bgcolor2')); 
      } 
     ); 
     jQuery('.submenu2').hover(
      function(){ 
       var $this = $(this); 
       $this.data('color', $this.css('color')).css('color', font); 
      }, 
      function(){ 
       var $this = $(this); 
       $this.css('color', $this.data('color')); 
      } 
     ); 
    }); 
    function convert(bg){ 
     bg = bg.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/); 
     return "#" + hex(bg[1]) + hex(bg[2]) + hex(bg[3]); 
    } 
    function hex(x) { 
     return ("0" + parseInt(x).toString(16)).slice(-2); 
    } 
</scri 
+0

你可以在bg上做一个console.log来查看它返回的结果吗? – Ohgodwhy

+0

当您在'ready'函数之外声明'bg'和'font',然后定义'convert'方法使用除“bg”之外的其他参数作为参数名称时会发生什么? – TLS

+0

@TLS我试过你的建议,它返回这''1'为空或不是一个对象' – Kiel

回答