您的位置:首页 > Web前端 > JavaScript

JavaScript调试问题

2016-01-12 16:44 555 查看

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的返回值用来确定浏览器的默认行为是否发生
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: