我有一個側欄,有時會比主要內容延伸得更遠(as here)。我想通過設置其最大高度以匹配主內容的高度來剪輯側邊欄的高度。
(注:我使用博客,所以固定HTML是更多的麻煩比它的價值這個這就是爲什麼我寧願找一個JS的解決方案)使用Javascript的兩個HTML元素的匹配高度
腳本,我試圖讓工作如下:
<!DOCTYPE html>
<html>
<style type="text/css">
.text1 {border:1px solid red;}
.text2 {border:1px solid blue;overflow:scroll;}
</style>
<body>
<div class="text1">Lorem ipsum dolor...</div>
<div class="text2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam quis massa sed mi viverra dapibus. Ut pulvinar a elit et sollicitudin. Aenean at augue dui. </div>
<script>
function sidebar(){
document.getElementsByClassName("text2").style.maxHeight = document.document.getElementsByClassName("text1").clientHeight;
}
</script>
</body>
</html>
['getElementsByClassName'](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName)返回實況['HTMLCollection'](HTTPS://顯影劑.mozilla.org/en-US/docs/Web/API/HTMLCollection),而不是數組。 – LGSon
@LGSon是的,你是對的。事實上,我的意圖是寫'像對象的數組',但我worte'數組',而不是:) –