2009-07-22 95 views
0

我的圖像翻轉作品...但只有一種方法。爲什麼我的圖像會以這種方式滾動?

function heartOver(id) 
{ 

    if(document.getElementById('heart' + id).src == 'images/heart.png'); 
    { 
     parent.document.getElementById('heart' + id).src = 'images/unheart.png'; 
    } 

    if(document.getElementById('heart' + id).src == 'images/unheart.png'); 
    { 
     parent.document.getElementById('heart' + id).src = 'images/heart.png'; 
    } 

} 


<img src="images/heart.png" id="heart'.$row['id'].'" alt="Love! width="16" height="16" border="0" onmouseover="heartOver'."('$row[id]')".'; return true;" onmouseout="heartOver'."('$row[id]')".'; return true;"> 

如果我註釋掉無論是IF語句的其他工作,但他們不會一起工作......

注:用else if沒有運氣嘗試這樣做。

想通了......杜:我有if();不;如果...

回答

5

把一個之間的其他 - 否則當第一個如果評估爲真,它會導致第二個如果被評估爲真也!

這裏有一個簡單的例子還檢查img元素,甚至存在的假設:

var img=document.getElementById('heart' + id); 
if (img) 
{ 
    if(img.src == 'images/heart.png') 
    { 
     img.src = 'images/unheart.png'; 
    } 
    else if(img.src == 'images/unheart.png') 
    { 
     img.src = 'images/heart.png'; 
    } 
} 
else 
{ 
    alert("Ooooh! No heart"+id+" element found!"); 
}  
+0

我試過了。它並沒有改變什麼.. – ian 2009-07-22 20:24:37

0

這是正確的嗎?

<img src="images/heart.png" id="heart'.$row['id'].'" alt="Love! width="16" height="16" border="0" onmouseover="heartOver'."('$row[id]')".'; return true;" onmouseout="heartOver'."('$row[id]')".'; return true;"> 

試試這個:

<img src="images/heart.png" id="heart<?php echo $row['id']; ?>" alt="Love!" width="16" height="16" border="0" onmouseover="heartOver(<?php echo $row['id']; ?>)" onmouseout="heartOver(<?php echo $row['id']; ?>)"> 
相關問題