JavaScript调试问题

本文探讨了JavaScript中通过element.style.width获取元素宽度时遇到的问题及原因,并提供了使用div.offsetWidth作为替代方案的方法。同时,文中还解释了事件处理器的返回值如何影响浏览器的默认行为。

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的返回值用来确定浏览器的默认行为是否发生

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值