当前位置: 代码迷 >> JavaScript >> JS获取元素的一些属性跟设置元素的宽度等属性
  详细解决方案

JS获取元素的一些属性跟设置元素的宽度等属性

热度:709   发布时间:2013-12-30 14:16:08.0
JS获取元素的一些属性和设置元素的宽度等属性
转】JS获取元素的一些属性和设置元素的宽度等属性
clientWidth   //获取元素的宽度
clientHeight  //元素的高度
offsetLeft   //元素相对于父元素的left
offsetTop  //元素相对于父元素的top
offsetWidth  //元素的宽度
offsetHeight //元素的高度
区别:
clientWidth=width+padding
clientHeight=height+padding
offsetWidth=width+padding+border
offsetHeight=width+padding+border
scrollLeft   //元素最左端到窗口最左端的距离
scrollTop  //元素最顶端到窗口最顶端的距离
scrollWidth  //元素的滚动宽度
scrollHeight  //元素的滚动高度

//获取元素的纵坐标
function getTop(e){
var offset=e.offsetTop;
if(e.offsetParent!=null) offset+=getTop(e.offsetParent);
return offset;
}
//获取元素的横坐标
function getLeft(e){
var offset=e.offsetLeft;
if(e.offsetParent!=null) offset+=getLeft(e.offsetParent);
return offset;
}

二、 设置元素的宽度属性
设置元素的宽度可用  document.getElementById('元素id值').style.width = 数值 + 'px';
同理,可设置高度等其他属性


如:

<div id="wrapper_imglist">
                                        <div id="scroller_imglist">
                                                <div id="thelist_imglist">
                                                        <div class="imgbox"><a href="#"><img src="images/1.jpg" /></a></div>
                                                        <div class="imgbox"><a href="#"><img src="images/1.jpg" /></a></div>
                                                        <!-- <div class="imgbox"><a href="#"><img src="images/1.jpg" /></a></div> -->
                                                       
                                                       
                                                </div>
                                        </div>
                                </div>       
                                <!--图片展示操作区-->
                                <div id="nav_imglist">
                                        <div id="prev_imglist" class="c_grey" onClick="main_pic_scroll.scrollToPage('prev', 0);">〈 上一张</div>
                                        <div class="indicator_num">
                                                <ul id="indicator_imglist">
                                                        <li class="active">1</li>
                                                        <li>2</li>
                                                        <!-- <li>3</li> -->
                                                </ul>
                                        </div>
                                        <div id="next_imglist" onClick="main_pic_scroll.scrollToPage('next', 0);">下一张 〉</div>
                                </div>

<script type="text/javascript">
var imgList = document.getElementById('thelist_imglist');
    var divNode = imgList.getElementsByTagName('img');
    var pageNum = divNode.length;//获得<img>元素个数
    var textList = document.getElementById('wrapper_imglist');
    var textListWidth = textList.offsetWidth;//获得元素宽度
   
    textListWidth = pageNum * textListWidth;
    var scroller_imglist = document.getElementById('scroller_imglist');
    scroller_imglist.style.width = textListWidth + 'px';//根据页数动态改变宽度

</script>

转自::http://blog.csdn.net/zhouruitao/article/details/5867626
  相关解决方案