2014-07-07 2 views
0

나는 동적으로 다른 사진 (P1)의 높이에 따라 그림 (P2)의 높이를 설정하기 위해 노력하고있어 : 기본적으로설정 사진 높이

<script> 
    function resizeElementHeight(element) { 
     var P1 = document.getElementById('P1'); 
     var P2 = document.getElementById('P2'); 
     if(P1.height != P2.height) { 
      P2.height = P1.height + "px"; 
     } 
    } 
</script> 

내가 "경우 말을하고 싶어 그림 P1의 높이가 그림 P2의 높이와 다른 경우 P2의 높이를 P1 "과 같게 설정하지만 작동하지 않습니다.

도와 주시겠습니까? 고맙습니다.

+0

DOM/이미지 준비 /로드 될 때 실제로 메서드를 호출합니까? –

답변

0

P2.height 대신 setAttribute 메서드를 사용해야합니다. P1.height도 동일하므로 getAttribute 메서드를 사용해야합니다.

그래서 P2.setAttribute('height', ...);

는 당신이 더 많은 지침이 필요하면 알려주세요.

+0

다음과 같은 의미입니까 : if (P2.height! = P1.height) { P2.setAttribute ('height', P1.height + "px"); } –

0

HTML에 height 속성이 없으면 그렇게 할 수 없습니다.

//<![CDATA[ 
var doc = document, bod = doc.body, IE = parseFloat(navigator.appVersion.split('MSIE')[1]); 
bod.className = 'js'; 
function gteIE(version, className){ 
    if(IE >= version)bod.className = className; 
} 
function E(e){ 
    return doc.getElementById(e); 
} 
function getPropertyFrom(p, e){ 
    return getComputedStyle(e).getPropertyValue(p) || e.currentStyle[p]; 
} 
E('P2').style.height = getPropertyFrom('height', E('P1'))+'px'; 
//]]> 

jQuery를 같이 다음과 같습니다 : 순수 자바 스크립트에서 창 크기 조정에

//<![CDATA[ 
$('#P2').css('height', $('#P1').height()+'px'); 
//]]> 

:

onresize = function(){ 
    E('P2').style.height = getPropertyFrom('height', E('P1'))+'px'; 
} 

창 순수 자바 스크립트를 사용하는 경우,이 같이해야 할 것이다 jQuery에서 크기 조정 :

$(window).resize(function(){ 
    $('#P2').css('height', $('#P1').height()+'px'); 
}); 

참고 : 외부 자바 스크립트를 사용해야하므로 캐시됩니다. window은 암시 적이므로 window.onresize = function(){}, window.document, window.open() 등을 쓸 필요가 없습니다. 원할 경우 window 개체를 참조 할 때 window을 그대로 두십시오.

+0

정말 자바 스크립트를 배워야합니다. – PHPglue