在JS中设置的高度有用转换成HTML后,高度失效了应该怎么改

html-css09

在JS中设置的高度有用转换成HTML后,高度失效了应该怎么改,第1张

那估计是因为在css也设定了img的高度,而css的优先级要比img本身的height属性要高,所以就失效了。建议改成内联的css,它的优先级是最高的:

height=\"39\"

改成

style=\"height:39px\"

<html>

<head>

</head>

<body style="height:400px">

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

<script type="text/javascript">

document.getElementById("div1").style.height=document.body.style.height

alert(document.getElementById("div1").style.height)

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

document.getElementById("div1").style.height=document.body.clientHeight

alert(document.getElementById("div1").style.height)

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

document.getElementById("div1").style.height=document.body.offsetHeight

alert(document.getElementById("div1").style.height)

</script>

</body>

</html>

设 你已经拿到高度 并将其赋值给JS变量 domHeight并获取了了需要赋值的标签dom

这个时候你只要获取你需要赋值的标签。

JQ:$(dom).height( domHeight )

javascript : dom.style.cssText += "height:100px"