2017-08-06 89 views
0

我創建了一個簡單的事件腳本,它在點擊按鈕時更改圖像。但不幸的是不改變,請幫助!提前致謝。事件在Javascript中無法正常工作

錯誤:我也沒有收到任何錯誤消息,只是它沒有改變圖像。

<html> 
<head> 
    <title>Events Practise</title> 
    <style> 
    #imtest{ 
    width:100px; 
    height:150px; 
    } 
    </style> 
</head> 
<body> 
    <h4> This a practise page of Events and event handlers </h4> 
    <p> Hi this the practise page that changes the Html and Css contents in the page by Using the JavaScript </p> 
    <img id="imtest" src="marni.jpg" alt="Image corrupted"> 
    <button onclick="eventtest()">Change Image</button> 
    <script> 
     function eventtest() 
     { 
       var imt = document.getElementById("imtest"); 
       imt.onclick = change; 
     } 
     function change() 
     { 
      var imtchng = document.getElementById("imtest"); 
      imtchng.src = "marni1.png"; 
     } 

    </script> 
</body> 

回答

0

I created a simple event script , which changes the image when clicked on the button

不,你已經創建了設定的圖像上單擊處理按鈕被點擊之後,當圖像被改變它會改變一個腳本。

如果您想直接通過點擊設置點擊處理器來更改圖像。

+0

謝謝您的澄清!現在我明白了我犯的錯誤:-) –

+0

@SatishMarni如果有幫助,請接受我已經接受的 – Ramanlfc

+0

的回答,但它表明「我沒有15個聲望點,所以選票被記錄下來」這樣的一些事情! ! :-) –

0

imt.onclick = change行中,您忘記了change後的括號。將其替換爲change()

0
<script> 
     function eventtest() 
     { 
change(); 
     } 
     function change() 
     { 
      var imtchng = document.getElementById("imtest"); 
      imtchng.src = "marni1.png"; 
     } 

    </script> 

or 

    <button onclick="change()">Change Image</button>