<!DOCTYPE?html>
<html>
<head>
????<meta?charset="UTF-8">
????<title>Water?Fall</title>
????<script?src="js/water.js"?type="text/javascript"></script>
????<style?type="text/css">
????*{
????????margin:?0;
????????padding:?0;
????}
????#main{
????????position:?relative;
????}
????.pin{
????????float:?left;
????}
????</style>
????</head>
<body>
????<div?id="main">
????</div>
</body>
</html>以上是HTML部分window.onload=function(){
var?oParent=document.getElementById("main");
for(var?i=0;i<98;i++){
var?oPin=document.createElement('div');
oPin.className='pin';
oParent.appendChild(oPin);
var?oBox=document.createElement('div');
oBox.className='box';
oPin.appendChild(oBox);
var?oImg=document.createElement('img');
oImg.src="images/"+i+".jpg";
oBox.appendChild(oImg);
}<!--for循環(huán)為批量添加本地圖片-->
waterfall(oParent);
}
function?waterfall(oParent){
var?_Pin=oParent.getElementsByClassName('pin');
var?_PinW=_Pin[0].offsetWidth;
console.log(_PinW);
}問題就出來了,獲取任意一個class名為pin的div的寬度或者高度時,獲取的值總為0,但用兼容模式獲取就正常,請教各位有沒有方法能解決這個獲取值不對的問題。感謝!
用JS添加的圖片在瀏覽器極速模式中用offset獲取高/寬結(jié)果為0
DoubleStone
2017-03-21 17:14:21