使用onmousedown获取您刚才所拥有的元素的ID?

Chr*_*ski 8 javascript onmousedown

这可能吗?

我正在尝试为onmousedown编写一个函数,该函数将返回您刚刚单击的元素的ID,以便稍后在另一个div中重新创建该元素时使用.

CMS*_*CMS 14

您可以使用事件委派,基本上只将一个事件处理程序连接到整个文档,并使用event.target获取最初调度该事件的元素:

document.body.onmousedown = function (e) {
  e = e || window.event;
  var elementId = (e.target || e.srcElement).id;

  // call your re-create function
  recreate(elementId);
  // ...
}

function recreate (id) {
  // you can do the DOM manipulation here.
}
Run Code Online (Sandbox Code Playgroud)

编辑:您可以通过以下方式为所有Scriptaculous draggables分配事件:

Event.observe(window, 'load', function () {
  Draggables.drags.each(function (item) {
    Event.observe(item.element, 'mousedown', function () {
      alert('mouseDown ' + this.id); // the this variable is the element 
    });                              // which has been "mouse downed"
  });
});
Run Code Online (Sandbox Code Playgroud)

点击这里查看示例.