2017-07-02 49 views
0

我試圖將點擊事件添加到地圖上的html區域。如果該區域被點擊,圖像應該顯示在這個區域。使用JavaScript將圖像添加到HTML區域標記

例如:

<area alt="" id="santiago" title="santiago" href="#" shape="poly" coords="225,692,281,695,277,744,225,743" /> 

的Javascript:顯示了

window.onload = function() { 

    document.getElementById("santiago").addEventListener("click", function (e) { 
     alert("CLICKED"); 
    var img = document.createElement('img'); 
    img.setAttribute('src', 'images/house.png'); 
    e.target.appendChild(img); 
    }); 

}; 

警報消息,如果被點擊的區域,但圖像不會出現。但它似乎被加載,因爲我可以在區域標記內的代碼中找到圖像。有人可以幫忙嗎?謝謝!

+0

您是否嘗試過追加創建的元素,而不是用於顯示的路徑? '.appendChild(img);' – NewToJS

+0

嗨,是的,這就是我以前試過的 - 現在改變了樣本。謝謝 – svb

+0

什麼是console.log(e.target)輸出? – liontass

回答

0

試試這個

var img = document.createElement('img'); 
$("#santiago")[0].appendChild(img); 
相關問題