내 웹 사이트에서 두 div가 동일한 길이가되도록 온라인에서 찾은 일부 코드가 있습니다. 그러나이 코드는 페이지가 새로 고쳐진 후에 만 작동하며이 문제를 일으키는 원인을 알지 못합니다. 어떤 도움을 주시면 감사하겠습니다!Javascript는 페이지 새로 고침 후에 만 작동합니다.
// EQUAL HEIGHTS
$.fn.equalHeights = function(px) {
$(this).each(function(){
var currentTallest = 0;
$(this).children().each(function(i){
if ($(this).height() > currentTallest) { currentTallest = $(this).height(); }
});
if (!px && Number.prototype.pxToEm) currentTallest = currentTallest.pxToEm(); //use ems unless px is specified
// for ie6, set height since min-height isn't supported
if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'height': currentTallest}); }
$(this).children().css({'min-height': currentTallest});
});
return this;
};
// just in case you need it...
$.fn.equalWidths = function(px) {
$(this).each(function(){
var currentWidest = 0;
$(this).children().each(function(i){
if($(this).width() > currentWidest) { currentWidest = $(this).width(); }
});
if(!px && Number.prototype.pxToEm) currentWidest = currentWidest.pxToEm(); //use ems unless px is specified
// for ie6, set width since min-width isn't supported
if ($.browser.msie && $.browser.version == 6.0) { $(this).children().css({'width': currentWidest}); }
$(this).children().css({'min-width': currentWidest});
});
return this;
};
// CALL EQUAL HEIGHTS
$(function(){ $('#equalize').equalHeights(); });
이 코드를 .ready 블록으로 사용하십시오. – underscore
스크립트를로드하는 순서를 확인하십시오. 함수를 사용하기 전에 함수에 사용 된 스크립트가로드되어 있습니까? –
@samitha'$ (function() {...})'은'$ (document) .ready (function() {...}) '와 같습니다. [here] (https://api.jquery.com/jquery/#jQuery3)를 참조하십시오. –