关于body调用javascript的方法,如:<body onload="check()">

关于body调用javascript的方法,如:<body onload="check()">,第1张

body是HTML的一个唯一固定元素!也就是说指执行一次当页面使用的时候就开始调用!所以我们常把一些一开始页面打开就要处理的动作放在body的onload中!

LZ所说的范围不只限于body内部而是使用于整个页面!

希望对你有帮助!

1、clientWidth / clintHeight

clientWidth  = 元素的宽度 + 元素的paddingLeft + 元素的paddingRight

clientHeight = 元素的高度 + 元素的paddingTop + 元素的paddingBottom

注意:如果该元素上存在上下滑动滚动条,则clientWidth的值不包括滚动条所占的宽度(即获得的clientWidth已经减去了滚动条的宽度)

注意:如果该元素上存在左右滑动滚动条,则clientHeight的值不包括滚动条所占的宽度(即获得的clientHeight已经减去了滚动条的高度)

2、clientTop / clientLeft

clientTop - 可视区域的上边距距离自身上边框的外边框的距离(即为上边框的宽度)

clientLeft - 可视区域的左边距距离自身左边框的外边框的距离(即为左边框的宽度)

没有滑动条的效果代码如下:

[html] view plain copy

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>获取元素的高度和宽度</title>

<style type="text/css">

#wrap{

height: 500px;

width: 500px;

background-color: skyblue;

margin: 0 auto;

border: 3px solid red;

overflow: scroll;

}

#content{

height: 200px;

width: 200px;

background-color: greenyellow;

margin: 0 auto;

border: 0px solid yellow;

border-width: 5px 6px 8px 12px;

padding: 5px 4px 6px 12px;

margin-top: 50px;

}

</style>

</head>

<body>

<div id="wrap">

<div id="content"></div>

</div>

</body>

<script type="text/javascript">

//获取content对象

var contentObj = documentgetElementById("content");

consolelog(contentObjclientHeight);

consolelog(contentObjclientWidth);

</script>

</html>  

以上结果输出的即为id为content的div的clientHeight 和 clientWidth 分别为 211 = height(200) + paddingTop(5) + paddingBottom(6)

有滚动条的代码如下,

在content div的里面添加一个id为one的div让新添加的div超出隐藏即可出现滚动条

[html] view plain copy

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>获取元素的高度和宽度</title>

<style type="text/css">

#wrap{

height: 500px;

width: 500px;

background-color: skyblue;

margin: 0 auto;

border: 3px solid red;

overflow: scroll;

padding: 5px;

}

#content{

height: 200px;

width: 200px;

background-color: greenyellow;

margin: 0 auto;

border: 0px solid yellow;

border-width: 5px 6px 8px 12px;

padding: 5px 4px 6px 12px;

margin-top: 50px;

overflow: scroll;

}

#one{

height: 300px;

width: 300px;

}

</style>

</head>

<body>

<div id="wrap">

<div id="content">

<div id="one"></div>

</div>

</div>

</body>

<script type="text/javascript">

//获取content对象

var contentObj = documentgetElementById("content");

consolelog(contentObjclientHeight);

consolelog(contentObjclientWidth);

consolelog(contentObjclientTop);

consolelog(contentObjclientLeft);

</script>

</html>  

最后输出的结果为clientHeight 和 clientWidth分别为 196 = height(200) + paddingTop(5) + paddingBottom(6) - 滚动条的宽度(15)

201 = width(200) + paddingLeft(12) + paddingRight(4) - 滚动条的宽度(15)

3、offsetHeight / offsetWidth

offsetHeight / offsetWidth实际上获取的内容和clientHeight / clientWidth的差别在于,offsetHeight和offsetWidth 不仅包括元素的高度和宽度和padding的值,而且包括border的宽度

注意:offsetHeight / offsetWidth包括滚动条的宽度(这一点与clientHeight / clientWidth)不同

[html] view plain copy

<!DOCTYPE html>

<html>

<head>

<meta charset="UTF-8">

<title>获取元素的高度和宽度</title>

<style type="text/css">

#wrap{

height: 500px;

width: 500px;

background-color: skyblue;

margin: 0 auto;

border: 3px solid red;

overflow: scroll;

padding: 5px;

}

#content{

height: 200px;

width: 200px;

background-color: greenyellow;

margin: 0 auto;

border: 0px solid yellow;

border-width: 5px 6px 8px 12px;

padding: 5px 4px 6px 12px;

margin-top: 50px;

overflow: scroll;

}

#one{

height: 300px;

width: 300px;

}

</style>

</head>

<body>

<div id="wrap">

<div id="content">

<div id="one"></div>

</div>

</div>

</body>

<script type="text/javascript">

//获取content对象

var contentObj = documentgetElementById("content");

consolelog(contentObjoffsetHeight);

consolelog(contentObjoffsetWidth);

consolelog(contentObjoffsetLeft);

consolelog(contentObjoffsetTop);

</script>

</html>  

输出的结果:offsetHeight = height(200) + paddingTop(5) + paddingBottom(6) + borderTop(5) + borderBottom(8)

offsetWidth = width(200) + paddingLeft(12) + paddingRight(4)  + borderLeft(12) + borderRight(6)

4、offsetTop / offsetLeft

offsetTop - 该元素的上边框的外边缘距离父级元素上边框的内边缘的距离

offsetLeft - 该元素的左边框的外边缘距离父级元素左边框的内边缘的距离

5、scrollHeight / scrollWidth

scrollHeight = 子级超出父级的元素的高度 + 父级的上下padding值

scrollWidth = 子级超出父级的元素的宽度 + 父级的左padding

6、scrollTop

scrollTop 元素滚动的距离

        //返回body下所有对象数组

        function getAll() {

            var objs = documentall;

            var length1;

            for (var i = 0; i < objslength; i++) {

                var obj = objs[i]tagName;

                if (obj == "body" || obj == "BODY") {

                    length1 = i+1;

                    break;

                }

            }

            var b_objs = new Array();

            for (var j = length1; j < objslength; j++) {

                b_objspush(objs[j]);

            }

            return b_objs;

        }

js没有提供获得body下的所有元素集合,只提供了获得全部标签的documentall

1、拖动后记录x,y值

给div加上mousePosition事件

function mousePosition(evt){  

evt = evt || windowevent;

return {

x : evtclientX + documentbodyscrollLeft - documentbodyclientLeft,

y : evtclientY + documentbodyscrollTop - documentbodyclientTop

}

}

2、打开页面div定位

$(“div”)attr("top",y)attr("left",x);

扩展资料

在用js获取元素的位置之前,元素在页面的位置的计算公式,如下:

元素在页面的位置=此元素相对浏览器视窗的位置+浏览器滚动条的值;

用getBoundingClientRect()方法来获得某个元素相对浏览器视窗的位置 {这个方法返回的是一个对象,即Object,该对象具有4个属性:top,left,right,bottom }。

<html >

<head>

<meta >

<title>Demo</title>

</head>

<body style="width:2000px; height:1000px;">

<div id="demo" style="position:absolute; left:518px; right:100px; width:500px; height:500px;

background:#CC0000; top: 114px;">Demo为了方便就直接用绝对定位的元素</div>

</body>

</html>

<script>

documentgetElementById('demo')onclick=function (){

if (documentdocumentElementgetBoundingClientRect) {

alert("left:"+thisgetBoundingClientRect()left)

alert("top:"+thisgetBoundingClientRect()top)

alert("right:"+thisgetBoundingClientRect()right)

alert("bottom:"+thisgetBoundingClientRect()bottom)

var X= thisgetBoundingClientRect()left+documentdocumentElementscrollLeft;

var Y = thisgetBoundingClientRect()top+documentdocumentElementscrollTop;

alert("Demo的位置是X:"+X+";Y:"+Y)

}

}

</script>

/

  @see 鼠标点击拖拽的效果(页面可以同时拖动多个框)

  @param boxId 整个对象(框)的id(一般为div或table)

  @param event 内置对象(必须传入)

 /

function mousePlead1(event, boxId) {

    var o = getO(boxId);

    var isIE = documentall  true : false;

    var e = event;

    var x = eoffsetX || elayerX;

    var y = eoffsetY || elayerY;

    documentonmousemove = function(e) {

        ostylefilter = 'Alpha(opacity=70)';

        ostyleopacity = '07';

        if (isIE) {

            osetCapture();

        } else {

            windowcaptureEvents(EventMOUSEMOVE);

        }

        var e = windowevent || e;

        if (eclientX - x >= 0 && eclientY - y >= 0 && eclientX - x <= getWinSize()[0] - getO(boxId)offsetWidth

                && eclientY - y <= getWinSize()[1] - getO(boxId)offsetHeight) {

            ostyleleft = (eclientX - x) + "px";

            ostyletop = (eclientY - y) + "px";

        }

    };

    documentonmouseup = function(e) {

        documentonmousemove = function() {

        };

        if (isIE) {

            oreleaseCapture();

        } else {

            windowreleaseEvents(oMOUSEMOVE);

        }

        ostylefilter = "";

        ostyleopacity = "";

    };

}

 

/

 @see 获得对象

 @param id 对象的id(表单元素和其他标签都可以)

 @return Object

/

function getO(id) {

    return documentgetElementById(id);

}

 

/

  @see 获得当前窗体的大小(width,height)

  @return Array 

 /

function getWinSize() {

    var width = parseInt(documentdocumentElementclientWidth);

    var height = parseInt(documentdocumentElementclientHeight);

    return new Array(width, height);

}

<html>

<head>

</head>

<body style="height:400px">

<div id="div1">12312</div>

<script type="text/javascript">

documentgetElementById("div1")styleheight=documentbodystyleheight;

alert(documentgetElementById("div1")styleheight);

//如果body,没有设置样式,这个高度就为空

documentgetElementById("div1")styleheight=documentbodyclientHeight

alert(documentgetElementById("div1")styleheight);

//如果设置了样式,offsetHeight就等于样式的height否则等于clientHeight

documentgetElementById("div1")styleheight=documentbodyoffsetHeight

alert(documentgetElementById("div1")styleheight);

</script>

</body>

</html>

以上就是关于关于body调用javascript的方法,如:<body onload="check()">全部的内容,包括:关于body调用javascript的方法,如:<body onload="check()">、JS中几种获取对象宽度和高度的区别、js 遍历body下的所有元素等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9303289.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-27
下一篇2023-04-27

发表评论

登录后才能评论

评论列表(0条)

    保存