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