Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用JavaScript检测动态变化的边距的大小?_Javascript_Jquery_Margin - Fatal编程技术网

如何使用JavaScript检测动态变化的边距的大小?

如何使用JavaScript检测动态变化的边距的大小?,javascript,jquery,margin,Javascript,Jquery,Margin,在我的html中,我有一个div,该div有一个随机的左边距: Html: 问题是,每当我试图检查利润率有多大时,就像这样: document.write($('#box1').css('margin-left')); 它始终显示0px,即使边距不同。 如果我尝试使用“If”语句进行检查,我会遇到同样的问题。您只需要更改文档。编写 $('#box1').css({'margin-left':Math.floor((Math.random()*450)+0)}); alert($('#box1'

在我的html中,我有一个div,该div有一个随机的左边距:
Html:

问题是,每当我试图检查利润率有多大时,就像这样:

document.write($('#box1').css('margin-left'));
它始终显示0px,即使边距不同。
如果我尝试使用“If”语句进行检查,我会遇到同样的问题。

您只需要更改
文档。编写

$('#box1').css({'margin-left':Math.floor((Math.random()*450)+0)});
alert($('#box1').css('margin-left'));

谢谢你们的帮助,我设法让它工作了。问题是我的document.write试图过早获取值。如果我添加这样的延迟,它会起作用:

setTimeout(show,500); 
function show() {
    document.write($('#box1').css('margin-left'));
};

为我工作:。也许你应该开始编写语法正确的HTML…不要使用
文档。写
。最好使用
窗口。getComputedStyle
来处理内联css和应用于elementWell的样式。。这并没有显示正确的值,而是将div一直向左移动,并返回0px。can't:/says“can't accpet your own answer in 2 days”噢,我不知道这种规则。
$('#box1').css({'margin-left':Math.floor((Math.random()*450)+0)});
alert($('#box1').css('margin-left'));
setTimeout(show,500); 
function show() {
    document.write($('#box1').css('margin-left'));
};