jQuery自动获取的高度怎么赋值给元素?

html-css010

jQuery自动获取的高度怎么赋值给元素?,第1张

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html,并引入jquery。

2、在index.html中的<script>标签,输入jquery代码:

var btmHeight = $(".bottomBox").height()

$(".footerImgList").css('height', btmHeight - 30 + 'px')

3、浏览器运行index.html页面,此时成功获取到h1标签的高度并减少30后赋值给footerImgList类的元素。

要写在onload事件方法中。因为你这样写,会在DOM节点(aaa)还没加载的时候执行,找不到aaa所以就不能改变其宽度

<script type="text/javascript">

function load(){

document.getElementById('aaa').style.width =100

}

</script>

</head>

<body onload="load()">

<div class="aaa" id="aaa"></div>

</body>