2013-01-05 82 views
0

我是编程和网页设计的新手,我正在尝试创建一个网站。我对于它的外观是计划this如何将javascript变量传递给外部css文件?

所以,我使用的代码是

<body> 
<div id="header"> 

</div> 
<div id="content"> 

</div> 
<div id="footer"> 

</div> 



<script> 
     function getWidth() 
     { 
      xWidth = null; 
      if(window.screen != null) 
       xWidth = window.screen.availWidth; 

      if(window.innerWidth != null) 
       xWidth = window.innerWidth; 

      if(document.body != null) 
       xWidth = document.body.clientWidth; 

      return xWidth; 
     } 
     function getHeight() { 
      xHeight = null; 
      if(window.screen != null) 
       xHeight = window.screen.availHeight; 

      if(window.innerHeight != null) 
       xHeight = window.innerHeight; 

      if(document.body != null) 
       xHeight = document.body.clientHeight; 

      return xHeight; 
     } 
</script> 
</body> 

后,我已经创建了一个.css文件(空的)。所以,我想创建一个网站,从读者的屏幕上获取可用的宽度和高度,然后在计算数字后显示相对于可用宽度和高度的div,例如,小盒子将具有height = width = 5%可用.width_of_screen。每个小盒子都将是一个项目或一个帖子。

所以,我的问题是:我如何传递变量的数据在外部的CSS来计算我的网站的数字?

感谢,加布里埃尔

+0

它更好地隐藏在JavaScript中的div而不是在CSS中进行更改。 – insomiac

+0

你是什么意思? – gabriel

+0

它很难通过javascript将值传递给外部css文件。如果你想对css进行更改,请从javascript或使用jquery进行更改。 – insomiac

回答

0

您可以设置使用jQuery像这样的CSS变量:

$("p").css({"background-color":"yellow","font-size":"200%"}); 

在这种情况下,你会用变量代替 “黄” 和 “200%”。

+1

对不起我的问题,但究竟是什么jQuery?这段代码适用于html文件吗?这是适用于外部的CSS文件? – gabriel

+0

@gabriel这是一个JavaScript库,它使得在某些情况下使用JavaScript更容易或更高效。 –

+0

@AlexW谢谢!那么,javascript可以在任何.css,.html文件中执行?我应该在哪里执行上面的代码? – gabriel