2015-06-26 34 views
-1

我的目標是讓底部的女王,一個創造的元素,可點擊與頂級女王一樣。如何做到這一點?如何讓createElement('IMG')可點擊?

<!DOCTYPE html> 
<html> 
<body> 
<p><button onclick='createAnImg()'>Create Card</button></p> 
<img src='Card Deck/QueenHearts.png' onclick='alert("You picked the Queen of Hearts!")'> 
<p id='card_holder'></p> 

<script> 
function createAnImg() { 
x = document.createElement('IMG'); 
x.setAttribute('id', 'Queen_of_Hearts'); 
x.setAttribute('src', 'Card Deck/QueenHearts.png'); 
x.setAttribute('width', '72'); 
x.setAttribute('alt', 'Card Face'); 
document.getElementById('card_holder').appendChild(x); 
} 
</script> 

</body> 
</html> 
+0

這裏的路徑的心像Picasa網絡相冊女王,如果你需要它: https://lh3.googleusercontent.com/-KBSwW1xXPt4/VY12U034XEI/AAAAAAAAASE/5C_uZ_o64QE/s96/ 23.png – JimM

回答

1

只需添加:

x.setAttribute('onclick', 'alert("You picked the Queen of Hearts!")'); 

你的屬性。

<!DOCTYPE html> 
 
    <html> 
 
    <body> 
 
    <p><button onclick='createAnImg()'>Create Card</button></p> 
 
    <img src='https://lh3.googleusercontent.com/-KBSwW1xXPt4/VY12U034XEI/AAAAAAAAASE/5C_uZ_o64QE/s96/23.png' onclick='alert("You picked the Queen of Hearts!")'> 
 
    <p id='card_holder'></p> 
 
    
 
    <script> 
 
    function createAnImg() { 
 
    x = document.createElement('IMG'); 
 
    x.setAttribute('id', 'Queen_of_Hearts'); 
 
    x.setAttribute('src', 'https://lh3.googleusercontent.com/-KBSwW1xXPt4/VY12U034XEI/AAAAAAAAASE/5C_uZ_o64QE/s96/23.png'); 
 
    x.setAttribute('width', '72'); 
 
    x.setAttribute('alt', 'Card Face'); 
 
    x.setAttribute('onclick', 'alert("You picked the Queen of Hearts!")'); 
 
    document.getElementById('card_holder').appendChild(x); 
 
    } 
 
    </script> 
 
    
 
    </body> 
 
    </html>

+1

工程就像一個魅力! – JimM

+1

很高興我能幫到你。 – Lance