2013-10-29 41 views
1

我已經拼湊出一個簡單的函數,它可以調整某些徽標的高度,因爲它們在高度上顯得太大。代碼在我第一次測試時工作得很好。但是現在它只能在每第3次點擊第4次點擊時才起作用。我懷疑它與文檔加載功能與圖像加載有關,但我不確定。關於文檔加載事件的Jquery偶爾會觸發

<script> 
// Get on screen image 
$(document).ready(function() 

{ 
$('#jobcontainer img').each(function() { 
     if($(this).height()>50) { 
     $(this).addClass('reduceheight') 
     } 
}) 

<style> 
.reduceheight { width:40%;} 
</style> 


<table id="jobcontainer" width="100%" border="0" cellpadding="0" cellspacing="0"> 
    <tr class="dknytliste"> 
     <td width="138" valign="top" class="dknytliste" style="padding-right:1em;"><p><a href="" class="dknytlink" title=""></a></p></td> 
     <td><a href="" class="dknytlink" title=""><img src="" /> </a></td> 
    </tr> 
    <tr> </tr> 
</table> 

看看這裏:http://dknyt.dk/forside/index2.php其右邊的jobcontainer。

幫助將非常apprciated。

+0

在什麼「點擊」,它只在頁面重新加載時才加載? – adeneo

+0

文檔就緒和窗口加載是兩種不同的東西 –

+0

@adeneo並不意味着點擊 - 意味着:重新加載頁面 –

回答

3

你把你的代碼document.ready,你應該使用承重:

$(window).on('load', function() { 

    $('#jobcontainer img').each(function() { 
     if($(this).height()>50) { 
     $(this).addClass('reduceheight'); 
     } 
}); 

而且,你有一些語法錯誤。你也忘卻了;.addClass('reduceheight')以及關閉事件文檔準備(現在的$(窗口)。在(「負荷」 ...)。

,你就忘記了</script>閉幕。

+0

非常感謝。我工作完美! –

+0

抱歉,我們沒有投票。 –

1

而不是文件準備不等待圖像加載使用window.onload

window.onload = function(){ 
    $('#jobcontainer img').each(function() { 
     if($(this).height()>50) { 
     $(this).addClass('reduceheight') 
    } 
} 
+0

哇,這很快。謝謝一堆。像魅力一樣工作。 –