JavaScript调试问题
获取element.style.width为空
请参考Width of a DIV is nothing according to Javascript, why?
new problem. document.getElementById().width returning unassigned. why
如:
<style>
div#foot_wrapper{
width:650px;
height:20px;
position:absolute;
bottom:10px;
background-color:#000000;
}
</style>
<script>
function align(div){
alert(div.style.width); // ---------------- box pops up blank?
div.style.left = (window.innerWidth/2) - (div.style.width/2);
}
</script>
alert(div.style.width)
弹出为空
主要原因为:
The style property only reflects the styles that were set inline on an element (using the style attribute on the element).
可以使用 div.offsetWidth
来代替
return vs no return
参考:Javascript onclick return functionality
如下:
<input type="checkbox" onclick="doAlert()" />
与
<input type="checkbox" onclick="return doAlert();" />
与<a href='#' onclick='someFunc(3.1415926); return false;'>Click here !</a>
类似
The return value of an event handler determines whether or not the default browser behaviour should take place as well.
event handler的返回值用来确定浏览器的默认行为是否发生