Js dynamically adds images to td of the table and registers events

  • 2020-03-30 03:18:02
  • OfStack

In this case, there is an extra case that responds to an event when a picture is clicked. The code is as follows:
 
<script type="text/javascript"> 
function onloadEvent(){ 
var _td = document.getElementById("a1"); 
var _img = document.createElement("img"); 
_img.setAttribute("id", "floatImage"); 
_img.setAttribute("src", "icon.png"); 
_img.setAttribute("onclick", "clickImage()"); 
_img.onclick = function(){ //Respond to events when clicked
alert(" Click on the "); 
//Window. The location = "http://baidu.com"; Jump the url
}; 
_td.appendChild(_img); 
} 
</script> 

The onload event in the body calls the above function.

Related articles: