问题描述
在JavaScript中,将一个元素的scrollHeight设置为另一个元素的scrollHeight的正确方法是什么?直接分配无效。
谢谢,Greg
In JavaScript what's the right way to set the scrollHeight of one element to that of another element? Direct assignment has no effect.Thanks, Greg
推荐答案
这是不可能直接的。 scrollHeight是一个只读属性,包含元素内容的总高度(以像素为单位)。
It's not possible directly. The scrollHeight is a read only property that contain the total height of element content in pixels.
如果有元素A并且你希望元素B具有与元素A相同的scrollHeight,那么使元素B具有单个子DIV元素(移动所有以前的元素)元素B内容作为DIV的子节点,设置为:
If have element A and you want to have element B with the same scrollHeight as element A, make it so that element B has a single child DIV element (move all previous element B content as child nodes of the DIV) that is set to:
width : 100%;
overflow : hidden;
并使用javascript将DIV的高度设置为元素A的scrollHeight(以像素为单位):
and using javascript set the height of DIV to scrollHeight of element A (in pixels):
document.getElementById('B').childNodes.item(0).style.height = document.getElementById('A').scrollHeight + 'px';
这篇关于JavaScript设置scrollHeight的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!