![](/img/trans.png)
[英]Setting a height on an element to match another element using javascript
[英]Setting height of column to another for each element
可悲的是,我需要使用JS将col-1
(静态)设置为与文本容器col-2
(动态)相同的高度。
我试过了:
$(function () {
var article = $('section.inner-services article');
$(article).each(function( index ) {
var textColHeight = $('section.inner-services article .col-2').height();
var figureCol = $('section.inner-services article .col-1');
$(figureCol).css('height', textColHeight);
console.log(index);
});
});
我的HTML标记是(简体):
<section class="inner-services">
<article>
<col-1>
<col-2>
</article>
</section>
这不会按预期方式遍历每篇article
,并将col-1
height设置为col-2
的高度。
如果可能,请简要说明我哪里出了问题。
您需要在每篇article
下查找col-2
和col-1
$(function () {
var article = $('section.inner-services article');
$(article).each(function( index ) {
var textColHeight = $(this).children('.col-2').height();
var figureCol = $(this).children('.col-1');
$(figureCol).css('height', textColHeight);
console.log(index);
});
});
首先,一旦创建了jQuery对象,就无需再次创建它,因此不需要$(article)
。
在第二里面each
可以参考当前元素为this
。
正确的代码可能如下所示:
$(function () {
$('section.inner-services article').each(function( index ) {
var textColHeight = $(this).find('.col-2').height();
var figureCol = $(this).find('.col-1').height(textColHeight);
console.log(index);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.