我的目标是浏览一组父包装器并获取子div.left的高度值并将div.right设置为相同的值。代码类型有效,但有一个例外,当我有多个父包装时,它们都会变成相同的高度而不是单独调整。
关于我做错的任何指示?我应该$('.paragraphs-item-text-with-image div.content').each(function(){
代替 - 如果是,我如何在使用$(this)
时定位div.left / right?
function myfunction() {
var contentHeight = 0;
$('.paragraphs-item-text-with-image div.left').each(function(){
contentHeight = $(this).height();
$('#page_paragraphs_wrapper .paragraphs-item-text-with-image .right img').height(contentHeight);
console.log('biggestHeight'+contentHeight);
});
};
每个父母的HTML结构基本上是:
<div id="paragraphs-item-text-with-image">
<div class="content">
<div class="left">
Text content
</div>
<div class="right">
<img src="">
</div>
</div>
</div>
答案 0 :(得分:0)
试试这个,
function myfunction() {
var contentHeight = 0;
$('.paragraphs-item-text-with-image div.left').each(function(){
contentHeight = $(this).height();
$(this).parent().find('div.right img').height(contentHeight);
console.log('biggestHeight'+contentHeight);
});
};
或者你可以使用.closest(selector)as,
function myfunction() {
var contentHeight = 0;
$('.paragraphs-item-text-with-image div.left').each(function(){
contentHeight = $(this).height();
$(this).closest('.paragraphs-item-text-with-image').find('div.right img').height(contentHeight);
console.log('biggestHeight'+contentHeight);
});
};