【问题标题】:How do I assign the value of a stylesheet element to a variable?如何将样式表元素的值分配给变量?
【发布时间】:2014-03-12 22:51:43
【问题描述】:

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

目前我只是想让“framewidth”获取样式表宽度的值,然后在屏幕上打印该值......一旦我知道我有这个值,我就可以使用它了。

我已经阅读了几十篇文章并在这上面花了 10 多个小时,这就是我目前所拥有的一切......

(我讨厌成为一个菜鸟哈哈)

“调整大小.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>

“调整大小.css”

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

【问题讨论】:

    标签: javascript php html css stylesheet


    【解决方案1】:

    .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

    【讨论】:

    • omg,那是我尝试的第一件事......然后我一定在其他地方遇到了不同的错误 - 谢谢:)
    【解决方案2】:

    最终工作的粗略版本...

    “调整大小.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 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>
    

    “调整大小.css”

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

    谢谢阿伦·P·约翰尼 https://stackoverflow.com/users/114251/arun-p-johny

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 2021-12-27
      • 2019-12-14
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多