2012-12-12 18 views
11

我用下面的代碼具有相等的高度rightposleftposmiddlepos的div:JQuery的div.height是錯誤的Chrome瀏覽器進行所有的div相同的高度

<script> 

    jQuery.noConflict(); 
    jQuery(document).ready(function($){ 

     // Find the greatest height: 
     maxHeight = Math.max($('#rightpos').height(), $('#middlepos').height()); 
     maxHeight = Math.max(maxHeight, $('#leftpos').height()); 

     // Set height: 
     $('#rightpos').height(maxHeight); 
     $('#middlepos').height(maxHeight); 
     $('#leftpos').height(maxHeight); 

    }) 
</script> 

使用這個最高的div確定主要頁面http://yaskawa.ir/的方式在Firefox中運行良好,但在Chrome中遇到問題。


更新1Sparky672的回答是:

我可以看到,這段代碼中,alert('test here');末不起作用。

<script> 
    jQuery.noConflict(); 
    //jQuery(document).ready(function($){}); 

    jQuery(window).load(function($){ 

     // Find the greatest height: 
     maxHeight = Math.max($('#rightpos').height(), $('#middlepos').height()); 
     maxHeight = Math.max(maxHeight, $('#leftpos').height()); 

     // Set height: 
     $('#rightpos').height(maxHeight); 
     $('#middlepos').height(maxHeight); 
     $('#leftpos').height(maxHeight); 

     alert('test here'); 
    }) 
</script> 

enter image description here

+0

我看到所有的div的有(使用Chrome)相同的高度。有什麼問題? – Kedume

+0

我在Linux中使用鉻,它有問題。我現在將上傳照片 –

+0

請構建你的問題的jsFiddle。 'window.load'在這裏工作正常:http://jsfiddle.net/snBLP/3/ – Sparky

回答

19

代替document.ready()嘗試window.load()到確保在計算高度之前加載所有資產。

jQuery(window).load(function($){ 

文檔:

負載事件被髮送到一個元件時,它和所有的子元件已經 被完全加載。此事件可以發送到與URL相關聯的任何元素 :圖像,腳本,框架,iframe和窗口對象。

http://api.jquery.com/load-event/

觀看演示:

http://jsfiddle.net/snBLP/3/

+0

謝謝@ Sparky672,但它不起作用,它似乎損壞了我的代碼。最後的「警報」不起作用。 –

+0

@smhnaji,[我的回答中沒有什麼不尋常的東西](http://jsfiddle.net/snBLP/2/)。向下滾動[此頁面並查看第一個示例代碼](http://api.jquery.com/load-event/)。你能構建一個jsFiddle演示來展示你的問題嗎? – Sparky

+1

這很奇怪,但是當使用'jQuery(window).load'時,我發現'$'不起作用(而'document.ready'正確使用'$'和相同的代碼),反正我用' jQuery'而不是'$'與您的解決方案。現場的例子是在http://yaskawa.ir/ –

-1

$(文件)。就緒()是不適合用於計算高度。 $(文件)。就緒()不會等待圖像加載等

我建議嘗試

$(window).ready() 

,而不是...

+0

你是對的。 RTM - > http://api.jquery.com/ready/ – Sparky

+0

你可以給我點我的回Sparky嗎?我個人使用過$(window).ready()...,它和$(window)一樣好.load() – rnirnber

+0

你可能認爲它很好用,但它不是一回事,而你的答案和'document.ready'沒有多大區別。再次,請閱讀'.ready()'... _「的文檔。」指定一個函數在DOM滿載時執行「。而'.load()'_「在它和所有子元素已經完全加載時發送給元素。」_ – Sparky

相關問題