2013-04-16 77 views
0

不幸的是,我需要使用JS将col-1(静态)设置为与文本容器col-2(dyanmic)相同的高度。将列的高度设置为每个元素的另一个

我已经试过:

$(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高度设置为col-2高度。

如果可能,请简要说明我出错的地方。

回答

0

起初,一旦你已经创建了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); 

    }); 

}); 
+0

这个工作,但我不得不做出一个编辑:) – SMacFadyen

+0

@SMacFadyen,是啊,我总是忘了'这''是不是'每个'的jQuery对象,直到我得到相应的错误。 – Andrei

0

你需要寻找col-2col-1下每article

$(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); 

    }); 

}); 
+0

你好,抱歉,这不工作:) – SMacFadyen

相关问题