最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to set marginLeft property using JavaScript? - Stack Overflow

programmeradmin3浏览0评论

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

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

However getting an error like 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>

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

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

However getting an error like 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>
Share Improve this question edited Dec 6, 2011 at 13:29 Jithu asked Dec 6, 2011 at 11:50 JithuJithu 5114 gold badges11 silver badges27 bronze badges 5
  • Sorry I was not knowing Jquery at all, i am using this for my Sharepoint 2010 master page !!I dont know what you meant by tagged JQuery!! – Jithu Commented Dec 6, 2011 at 11:56
  • Your headline says "JQuery - How to set Margin left property", does it not? – Paparappa Commented Dec 6, 2011 at 11:59
  • @alex let me put full code . not sure with JQuery, First time using it!! – Jithu Commented Dec 6, 2011 at 12:08
  • @alex Plz have a look at the code, I thought it is a jquery !! Plz suggest if not!! – Jithu Commented Dec 6, 2011 at 12:13
  • @Emreerik can anyone tell me, my question is for jQuery or JavaScript, I thought I seen some JQquery functions, that's why I put – Jithu Commented Dec 6, 2011 at 12:18
Add a comment  | 

6 Answers 6

Reset to default 5

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

<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 style property on a null object.

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

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

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

I assume you call document.getElementById before the element is available.

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

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 load event handler (you would overwrite it).

Something like this possibly?

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

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

$('#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.

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.

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

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

.css Api of jQuery

发布评论

评论列表(0)

  1. 暂无评论