如何将样式表元素的值赋给变量?

时间:2014-03-12 22:51:43

标签: javascript php html css stylesheet

我在点击链接时调整了DIV的大小,但我确实需要将值赋给变量,之后我可以更改值...最终允许[+]和[ - ]按钮用户一次增加和减少1个像素的大小。

此刻,我只是想做出#34; framewidth"拿起样式表宽度的值,然后在屏幕上打印这个值...一旦我知道我有这个值,我就可以玩它。

我已经阅读了数十篇文章并花费了超过10个小时的时间,这就是我到目前为止所做的一切......

(我讨厌做一个菜鸟大声笑)

" resize.php"

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
 <link rel="stylesheet" href="resize.css" type="text/css"/>
 <title>Resize</title>
</head>

<body>

<script type="text/javascript">
function frame700()
{
 var framewidth=document.getElementById('frame').style.width.value;
 document.getElementById('frame').style.width='700px';
 document.getElementById("info").innerHTML=framewidth;
}

function frame500()
{
 document.getElementById('frame').style.width='500px';
}
</script>

<div class="resize" ID="frame" style="width:500px"> 
 <table>
 <tr>
  <td>
   <a href="#" onclick="frame700()">700</a> <a href="#" onclick="frame500()">500</a><br/>
  </td>
 </tr>
 <tr>
  <td id="info"></td>
 </tr>
</table> 

</div>

</body>
</html>

&#34; resize.css&#34;

div.resize
{
 position:absolute;
 height:100px;
 background-color:#ffc080;
 border:2px solid orange;
}

2 个答案:

答案 0 :(得分:3)

.style.width会为您提供元素的宽度,但没有.value属性。

应该是

var framewidth = document.getElementById('frame').style.width;

演示:Fiddle


您可能还想查看window.getComputedStyle()

function frame700() {
    var el = document.getElementById('frame'),
        style = window.getComputedStyle ? window.getComputedStyle(el) : el.currentStyle || {};
    var framewidth = style.width;
    document.getElementById('frame').style.width = '700px';
    document.getElementById("info").innerHTML = framewidth;
}

演示:Fiddle

答案 1 :(得分:0)

最终工作原油版......

&#34; resize.php&#34;

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
 <link rel="stylesheet" href="resize.css" type="text/css"/>
 <title>Resize</title>
</head>

<body>

<script type="text/javascript">

function widthD()
 {
  var el = document.getElementById('frame'), style = window.getComputedStyle ? window.getComputedStyle(el) : el.currentStyle || {};
  var frameWpx = style.width;
  var frameW=parseInt(frameWpx);
  frameW=frameW-1;
  frameWpx=frameW+"px";
  document.getElementById('frame').style.width = frameWpx;
 }

function widthU()
 {
  var el = document.getElementById('frame'), style = window.getComputedStyle ? window.getComputedStyle(el) : el.currentStyle || {};
  var frameWpx = style.width;
  var frameW=parseInt(frameWpx);
  frameW=frameW+1;
  frameWpx=frameW+"px";
  document.getElementById('frame').style.width = frameWpx;
 }

function heightD()
 {
  var el = document.getElementById('frame'), style = window.getComputedStyle ? window.getComputedStyle(el) : el.currentStyle || {};
  var frameHpx = style.height;
  var frameH=parseInt(frameHpx);
  frameH=frameH-1;
  frameHpx=frameH+"px";
  document.getElementById('frame').style.height = frameHpx;
 }

function heightU()
 {
  var el = document.getElementById('frame'), style = window.getComputedStyle ? window.getComputedStyle(el) : el.currentStyle || {};
  var frameHpx = style.height;
  var frameH=parseInt(frameHpx);
  frameH=frameH+1;
  frameHpx=frameH+"px";
  document.getElementById('frame').style.height = frameHpx;
 }

</script>

<div class="resize" ID="frame"> 
 <table>
 <tr>
  <td></td>
  <td><a href="#" onclick="heightD()">D</a></td>
  <td></td>
 </tr>
 <tr>
  <td><a href="#" onclick="widthD()">D</a></td>
  <td id="info"></td>
  <td><a href="#" onclick="widthU()">U</a></td>
 </tr>
 <tr>
  <td></td>
  <td><a href="#" onclick="heightU()">U</a></td>
  <td></td>
 </tr>
</table> 

</div>

</body>
</html>

&#34; resize.css&#34;

div.resize
{
 position:absolute;
 width:100px;
 height:100px;
 background-color:#ffc080;
 border:2px solid orange;
}
谢谢Arun P Johny https://stackoverflow.com/users/114251/arun-p-johny