setAttribute为什么无法改变元素的宽高

代码如下
1.使用oUl.style.height='1000px'可以改变ul的高度,但是为什么使用oUl.setAttribute('height','1000px')却无法改变;
2.将“height”改成title后,也是有效果的。

<script>
    window.onload=function(){
        var oUl=document.getElementById('ul');
        oUl.setAttribute('height','1000px');
        // oUl.style.height='1000px';
    }
</script>

<body>

<ul id="ul">
    <li>1</li>
    <li>2</li>
    <li>3</li>
</ul>

</body>

阅读 5.3k
4 个回答

setAttribute()是用来设置element的属性值的,element没有height属性,你可以试一下oUI.setAttribute('style', 'height: 1000px');

ul是没有height属性的但是有title属性

ul有height属性吗?

用 setAttribute 改行内样式应该这么写.

oUl.setAttribute('style', 'height: 200px;');

不过这样会覆盖掉原来 style 行内样式.

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题