代码之家  ›  专栏  ›  技术社区  ›  Cheetah

如何在IE中设置对象的边界?

  •  26
  • Cheetah  · 技术社区  · 16 年前

    我正在尝试从javascript设置对象的边界。我可以在Opera&firefox中完成,但在Internet Explorer中代码不起作用。

    以下是我的javascript:

    function SetTopMargin (ObjectID, Value)
    {
        document.getElementById(ObjectID).style.marginTop =  Value.toString() + "px";
    }
    

    它被称为:

    SetTopMargin("test_div_id", 100);
    

    有人知道在Internet Explorer中可以使用的代码吗?

    3 回复  |  直到 7 年前
        1
  •  33
  •   robinCTS    7 年前

    [2016年更新]在所有当前浏览器(包括IE8+)上,您的代码

    document.getElementById(ObjectId).style.marginTop = Value.ToString() + 'px';
    

    工作良好。

    很老 IE(<8)版本,您必须使用此非标准装置:

    document.getElementById(ObjectId).style.setAttribute(
       'marginTop', Value.ToString() + 'px');
    

    编辑 -从OP删除的评论:

    请注意,虽然您可以在当前IES中使用style.setattribute(“Margin-Top”,…),但8及更旧版本需要style.setattribute(“MarginTop”,…)

        2
  •  4
  •   JamesEggers    16 年前

    你的代码在IE8中对我有效。

    <html>
      <head>
        <script type="text/javascript"> 
        function SetTopMargin (ObjectID, Value)
        {   
          document.getElementById(ObjectID).style.marginTop =  Value.toString() + "px";
        }
        </script>
     </head>
     <body>
       <button id="btnTest" onclick="SetTopMargin('btnTest', 100);">Test</button>
     </body>
    </html>
    

    在IE6中,经过很短的停顿之后,它似乎也在工作。

        3
  •  -3
  •   Chuck Norris Rohidas Kadam    13 年前

    首先,您应该使用像jquery或dojo这样的JavaScript库。我还推荐www.debugbar.com检查IE的DOM。

    关于你的问题, elem.style = "margin: 10px" 应该在IE中工作。

    希望这有帮助!