如何使用 JavaScript 设置 marginLeft 属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8399446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:51:20  来源:igfitidea点击:

How to set marginLeft property using JavaScript?

javascript

提问by Jithu

I used the following code to set the left margin for a div with id=""

我使用以下代码为 div 设置左边距 id=""

document.getElementById('s4-ca').style.marginLeft = '0px';

However getting an error like Object Required!

但是得到一个错误,如 Object Required!

UPDATE - My code

更新 - 我的代码

<script type="text/javascript">
    var groupName;
    $().SPServices({
    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: false,
    completefunc: function(xData, Status) {
    $(xData.responseXML).find("[nodeName=Group]").each(function()
    {
    groupName = $(this).attr("Name");
    });
    }
    });
    if($.trim(groupName) != 'ABC')
    {
    document.getElementById('s4-leftpanel').style.display = 'none';
    document.getElementById('s4-ca').style.marginLeft = '0px';
    }
</Script>

采纳答案by Chris Fulstow

There's nothing wrong with your JavaScript, just give the <div>a matching idproperty, then it should work fine, i.e.

你的 JavaScript 没有任何问题,只要给出<div>一个匹配的id属性,它应该可以正常工作,即

<div id="s4-ca"></div>

So there reason you're getting the error is because document.getElementById('s4-ca')doesn't find a en element with a matching id, so it's null, and you can't reference the styleproperty on a null object.

因此,您收到错误的原因document.getElementById('s4-ca')是没有找到具有匹配 的 en 元素id,因此它是null,并且您无法style在空对象上引用该属性。

Also, when you set the marginLeftthere's no need to specify units for zero, you can use just 0.

此外,当您设置 时,marginLeft无需为零指定单位,您可以只使用0.

document.getElementById('s4-ca').style.marginLeft = "0";

回答by Felix Kling

Not reproducible: http://jsfiddle.net/VH2z2/

不可重现:http: //jsfiddle.net/VH2z2/

I assume you call document.getElementByIdbeforethe element is available.

我假设您document.getElementById元素可用之前调用。

Either include the code on the bottom of the page or in the loadevent handler, e.g.

在页面底部或load事件处理程序中包含代码,例如

window.onload = function() {
    document.getElementById('s4-ca').style.marginLeft = '0px';
};

You should not do this if there is other JavaScript code which might set a loadevent handler (you would overwrite it).

如果存在其他可能设置load事件处理程序的JavaScript 代码(您会覆盖它),则不应这样做。

回答by dov.amir

the problem is "object requierd" that means that

问题是“对象要求”,这意味着

document.getElementById('s4-ca')

returns nothing, you must make sure an element with id 's4-ca' exists

不返回任何内容,您必须确保存在 id 为 's4-ca' 的元素

回答by Paparappa

Something like this possibly?

有可能是这样的吗?

$('#s4-ca').css({ marginLeft: 0 });

回答by Willem

$('#s4-ca').css({ marginLeft: 0 });

or

或者

$('#s4-ca').css('margin-left', 0 );

your code is not jquery, but the code above is the way to do this in jquery, both work.

您的代码不是 jquery,但上面的代码是在 jquery 中执行此操作的方法,两者都有效。

update: your error message probably means that there is no element with id="s4-ca", please check for such an element! see this jsfiddle: http://jsfiddle.net/4DzyW/where your code just works.

更新:您的错误消息可能意味着没有 id="s4-ca" 的元素,请检查这样的元素!看到这个 jsfiddle:http: //jsfiddle.net/4DzyW/,你的代码在那里工作。

回答by Govind Malviya

if you want to use jQuery (according to your question's tag)

如果你想使用jQuery(根据你问题的标签)

$("#s4-ca").css('marginLeft','0px');

.cssApi of jQuery

jQuery 的.cssApi