如何在IE中设置对象的边距? [英] How do I set the margin of an object in IE?

查看:82
本文介绍了如何在IE中设置对象的边距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从JavaScript设置对象的边距。我能够在Opera& Firefox,但代码在Internet Explorer中不起作用。

I am trying to set the margin of an object from JavaScript. I am able to do it in Opera & Firefox, but the code doesn't work in Internet Explorer.

这是我的JavaScript:

Here is the JavaScript I have:

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

它的调用如下:

SetTopMargin("test_div_id", 100);

所以有人知道一些可以在Internet Explorer中运行的代码吗?

So does anyone know some code that will work in Internet Explorer?

推荐答案

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

[Updated in 2016] On all current browsers (including IE8+), your code

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

工作正常。

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

On very old IE (< 8) versions, you must use this non-standard contraption instead:

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

编辑 - 来自OP删除的评论:

EDIT - From deleted comment by OP:


请注意,虽然您可以在当前IE中使用style.setAttribute('margin-top',..),但8及更早版本需要style.setAttribute('marginTop') ,..)

Note that while you can use style.setAttribute('margin-top', ..) in current IEs, 8 and older require style.setAttribute('marginTop', ..)

这篇关于如何在IE中设置对象的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆