1

在这段代码中:

$("div#info_holder").css("marginLeft", "100%");
alert(parseFloat($("#info_holder").css("marginLeft")));
alert(parseFloat(document.getElementById("info_holder").style.marginLeft));

第一个警报返回 1037.78,第二个警报返回 100

这是为什么?!

4

2 回答 2

1

这是因为 jQuery 版本将边距作为像素返回,而本机 JS 将值作为百分比返回。看一下这个小提琴,它在运行 parseFloat 之前对值进行了调整。

$("div#info_holder").css("marginLeft", "100%");

console.log($("#info_holder").css("marginLeft"));
console.log(document.getElementById("info_holder").style.marginLeft);

http://jsfiddle.net/ryanbrill/wtABu/

于 2013-12-23T17:29:32.053 回答
0

$("#info_holder").css("marginLeft")document.getElementById("info_holder").style.marginLeft以百分比读取时返回像素。

于 2013-12-23T17:36:12.090 回答