JS中的left,pixelLeft,posLeft,offsetLeft

来源:互联网 发布:进销存的软件 编辑:程序博客网 时间:2024/06/03 22:59

left(字符串,即系html中left的值.)
用法:
1、alert(document.getElementById('divid').style.left)  //没有设置则返回空
2、document.getElementById('divid').style.left = '100px';  //注意带上单位


pixelLeft(数值类型,将left的值(如果是空串则赋为0)转化为像素值.(不带单位))
用法:
1、alert(document.getElementById('divid').style.pixelLeft)
2、document.getElementById('divid').style.pixelLeft = 100;  //该属性只支持IE


posLeft(浮点类型,将left的值(如果是空串则赋为0)转化为数值.(不带单位))
用法:
1、alert(document.getElementById('divid').style.posLeft)
2、document.getElementById('divid').style.posLeft = 100;  //该属性只支持IE


offsetLeft(元素相对其定位的父级元素的偏移量,如果没有定位的父级元素,则相对于页面的偏移.)
用法:
1、alert(document.getElementById('divid').offsetLeft)  //返回数值


jquery1.2以后支持:$("#divid").offset().left
用法:
1、alert($("#divid").offset().left)