在此代码中:
$("div#info_holder").css("marginLeft", "100%");
alert(parseFloat($("#info_holder").css("marginLeft")));
alert(parseFloat(document.getElementById("info_holder").style.marginLeft));
第一个警报返回 1037.78,第二个警报返回 100
为什么?!
这是因为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/
$("#info_holder").css("marginLeft")
返回像素,同时document.getElementById("info_holder").style.marginLeft
以百分比读取它。