如何使用JavaScript检测动态变化的边距的大小?
在我的html中,我有一个div,该div有一个随机的左边距:如何使用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: 问题是,每当我试图检查利润率有多大时,就像这样:
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'));
};