用JS改变保证金

时间:2013-10-17 19:05:58

标签: javascript jquery css

我想在给定的div上留下动态余量。

我试过这段代码:

window.onload = function()
{
    var pageWidth = window.innerWidth; 
    var size = (pageWidth - 200 ) / 2;
    $('#search').css('margin-Left', size));
    alert(size);
 };

哪个不起作用(这里的警报仅用于测试目的),问题来自ligne $('#search').css('margin-Left', size));,但我找不到...

我尝试在左边缘使用小写l并且无效。

4 个答案:

答案 0 :(得分:3)

以下是否适合您?

document.getElementById("search").style.marginLeft = size + "px";

答案 1 :(得分:0)

尝试使用左边距离,不带连字符。

jQuery.css()文档:http://api.jquery.com/css/

答案 2 :(得分:0)

您需要指定要使用的单位类型。

$('#search').css('margin-left', size + 'px');

答案 3 :(得分:0)

JAVASCRIPT

$(document).ready(function(e){
    alert('test');
    var pageWidth = parseInt($(window).innerWidth());
    alert(pageWidth);
    var size = (pageWidth - 200 ) / 2;
    $('#search').css('margin-left', size);
    alert(size);
})

HTML

<div id = 'search'></div>

CSS

#search
{
    height:100px;
    width:100px;
    background-color:#F00;
}

FIDDLE LINK