如:
<script> <BR>var elem = document.getElementById('demo'), <BR>width = elem.style.width; <BR>alert(parseInt(width));// 24 <BR></script>
可是如果css值是这样呢:
<script> <BR>var elem = document.getElementById('demo'), <BR>opacity = elem.style.opacity; <BR>alert(parseInt(opacity));// 0 <BR></script>
以上显然parseInt()会出错,哪怕是'.5′这样的值,如果换成parseFloat()将能得到正确的结果:
<script> <BR>var elem = document.getElementById('demo'), <BR>opacity = elem.style.opacity; <BR>alert(parseFloat(opacity));// 0.5 <BR></script>
两者有何区别?
parseInt() 返回一个整数
parseFloat() 返回一个浮点数
进一步了解
parseInt 和
parseFloat