2014-09-21 6 views
0

상자 높이를 오버플로 된 콘텐츠 높이로 조정하여 넘치는 내용을 해결하려고하지만 트릭을 수행하지 않는 것 같습니다. height()가 요소의 정확한 높이를 반환하지 않습니다.

  if ($('.content-right').outerHeight() > $('.content-right').parent().parent().height()) { 
       $('.content-right').parent().parent().height($('.content-right').outerHeight(true)); 
      } 
      console.log('Box Height: ' + $('.content-right').parent().parent().height()); 
      console.log('Content Height: ' + $('.content-right').height()); 

이 뜻을 출력 사업부 명확하게 아래의 예를 많이 클로 잘못

Box Height: 599 
Content Height: 594 

. 어떤 아이디어? 이미지 형태로

Problem Area 문제 영역 : http://prntscr.com/4p1obb

+0

의 높이를 제공합니다 http://api.jquery.com/height/ 그것은 분명히 말한다 : *'이 방법은 어떤 arguments.'를 허용하지 않습니다 * –

+1

'.parent(). parent()'를 사용하는 대신 http://api.jquery.com/closest/ 메서드를 살펴보십시오. –

+0

또한, ** 더 많은 것을 한 번에 너무 많이 사용하려고하는 요소 **를 캐시하십시오. 'var $ contRight = $ ('. content-right ');' –

답변

1

당신이 매개 변수를 전달하지 않는 경우는 높이를 반환하지 않습니다 이전 버전의 버그 jQuery를 outerHeight이 있습니다. 또한 주석에서 제안한대로 height()에서 true를 제거해야합니다.

if ($('.content-right').outerHeight(true) > $('.content-right').parent().parent().height()) { 
    $('.content-right').parent().parent().height($('.content-right').outerHeight(true)); 
} 
console.log('Box Height: ' + $('.content-right').parent().parent().height()); 
console.log('Content Height: ' + $('.content-right').height()); 

UPDATE :

이 시도 (그리고 위에서 언급 한 버그는 계속 적용되므로 매개 변수에 넣어해야합니다). 주의 깊게 문서를 읽을 경우 아래의 코드는 나에게 868

var outerHeight = 0; 
$('.content-right > *').each(function() { 
    outerHeight += $(this).outerHeight(true); 
}); 
console.log(outerHeight); 
+0

선택자를 캐시하십시오! '.parent()'를 불필요하게 중첩하는 대신'.closest ('selector')'메서드를 사용하십시오. –

+0

@Roko C. Buljan OP 코드를 최적화하는 것이 저의 목표는 아닙니다. 나는 그의 질문에 대한 대답을 제안하고있다. 위의 의견에서 이미 제안 했으므로 독자적으로 할 수 있습니다. – manishie

+0

로코, 치우친 종료. 그것은 그 기능과 관련이 없습니다. 그것은 개인적인 견해입니다. 명시된 바와 같이 디버깅을 시도하는 vars에서 다시 작성되었습니다. 페이지에 표시된대로 오류가 없습니다. 반환 된 높이가 정확하지 않습니다. ** – WASasquatch

관련 문제