如何使用Javascript(而不是html)使用onclick事件声明事件对象?

use*_*236 3 javascript events reference function onclick

这是我的代码:

function testimage() {
  var image;

  image = new Image();

  with (image) {
    id = "page1";
    border = 0;
    name = "IllustGIF";
    useMap = "#MAP1";
    src = "sample.gif" + "?d=" + new Date();

    // The event argument here is invalid; it's to show what is the desired result.
    onclick = function() { RecordMouseClick(event, this.id); };
  }

  // attached to body tag
  document.getElementById("body_ID").appendChild(image);
}

function RecordMouseClick(event, id) {
  alert("id: " + id + ", and event object should contain mouse x- and y- coordinates clicked");
}
Run Code Online (Sandbox Code Playgroud)

我需要在JavaScript中定义onclick事件,而不是HTML.现在在HTML中,它很简单 - 只需将事件作为参数传递给函数,然后将事件对象传递给JavaScript但是从头开始,所有这些都在JavaScript中,没有HTML,对我来说很有挑战性.当调用引用函数时,它应该记录元素id,并且鼠标单击onclick事件中的x和y坐标.

如何将事件传递给onclick,以使事件对象在运行时有效?

谢谢.

I a*_*ica 5

这是一个小提琴样本.

image.onclick = function(e) { 
    e = e || window.event;
    RecordMouseClick(e, image.id); 
};
Run Code Online (Sandbox Code Playgroud)