现在我正在处理窗口加载代码,但不能使它与窗口调整大小一起工作(如果我对调整大小事件重复相同的函数,高度值会增加)。数组是在onload完成后显示初始值。
var arr = [];
window.onload = function() {
var newsBlocks = document.getElementsByClassName('news');
for (var i = 0; i < newsBlocks.length; i++) {
var newsBlock = newsBlocks[i];
var left = newsBlock.getElementsByTagName('article')[0];
var right = newsBlock.getElementsByTagName('article')[1];
var height = Math.max(left.offsetHeight, right.offsetHeight);
left.style.height = right.style.height = height.toString() + 'px';
arr.push(left.style.height);
console.log(arr);
}
};
这是html:
<div class="main">
<section class="news">
<article>
<a href="#" class="news-image"><img src="images/t.png"></a>
<h2><a href="#">Heading</a></h2>
<a href="#" class="description">test text</a>
</article>
<article>
<a href="#" class="news-image"><img src="images/t.png"></a>
<h2><a href="#">Heading</a></h2>
<a href="#" class="description">a lot of test text here</a>
</article>
</div>
纯 JavaScript 解决方案值得赞赏。提前感谢您的帮助!