js offsetwidth获取DIV宽度无效

js offsetwidth获取DIV宽度无效,第1张

<style>

#div_001{

Width:1000px; min-Height:300px; Background:#bdb1bf;

}

</style>

<div id="div_001" ></div>

<script>

var o = documentgetElementById('div_001');

documentwrite(ooffsetWidth);

</script>

alert或者documentwrite都是正确的

没有你说的情况出现

<style type="text/css">

#div1{width:100px;height:100px;}

<style>

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

<div id="div2">3434</div>

<script>

function getStyle(obj,attribute){//获取样式

return objcurrentStyleobjcurrentStyle[attribute]/ie/:documentdefaultViewgetComputedStyle(obj,null)[attribute]/ie9 火狐 谷歌/;

}

var div1 = documentgetElementById("div1");

var div2 = documentgetElementById("div2");

var wid = getStyle(div1,"width")

div2onmouseover = function(){//鼠标移到div2时设置宽度

thisstylewidth = wid;

};

</script>

javascript获取div的class中设置的宽高度

高度(offsetHeight):

<head><title>用js获取div的高度</title></head>

<style  type="text/css">

#box{border:1px solid #ff0000;width:200px; }

</style>

<script language="javascript">

function jj(){

var pp=documentgetElementById("box")offsetHeight;

alert(pp);

}

</script>

<body>

<div id="box">

<p>段落内容</p><p>段落内容</p>

<input type="button" onclick="jj();" value="click">

</div>

</body>

宽度(offsetWidth):

<head><title>用js获取div的高度</title></head>

<style  type="text/css">

#box{border:1px solid #ff0000;width:200px; }

</style>

<script language="javascript">

function jj(){

var pp=documentgetElementById("box")offsetWidth;

alert(pp);

}

</script>

<body>

<div id="box">

<p>段落内容</p><p>段落内容</p>

<input type="button" onclick="jj();" value="click">

</div>

</body>

div中是在jsp中使用,用来设置区域上的效果,可以使用document对象来获得该div的id,进而得到该div所占据的宽度值,如下:

<div id="banner" style="width:100%">

js获取banner的宽度:documentgetElementById("banner")offsetWidth

html文件

<div id="aaa">

</div>

<script language="javascript" type="text/javascript" >

//获取到aaa的宽度,保存在变量a中

//ajax调用testphpa={js传值}

//ajax返回值在html页面处理显示

</script>

testphp文件

<php

//处理变量a

>

给div设置一个id或者class:

html部分:

<div id="mydiv"></div>

js部分:

<script>

//获取mydiv

var mydiv=documentgetElementById("mydiv");

//获取#mydiv的高度

var divheight=mydivstyleheight;

//设置mydiv行高

mydivstylelineHeight=divheight;

</script>

以上就是关于js offsetwidth获取DIV宽度无效全部的内容,包括:js offsetwidth获取DIV宽度无效、js 获取DIV宽 并赋值给另一个DIV、javascript如何获取div的class中设置的宽高度等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

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

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

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

发表评论

登录后才能评论

评论列表(0条)

    保存