使用onmousedown获取您刚才所拥有的元素的ID? [英] Use onmousedown to get the ID of the element you just mousedowned on?
本文介绍了使用onmousedown获取您刚才所拥有的元素的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这可能吗?
我正在尝试为onmousedown编写一个函数,该函数将返回您刚刚单击的元素的ID,以便稍后用于重新创建该元素一个不同的div。
I am attempting to write a function for onmousedown that will return the ID of the element you just clicked for later use in recreating that element in a different div.
推荐答案
你可以使用 event delegation ,基本上只将一个事件处理程序连接到整个文档,然后获取最初调度事件的元素,使用 event.target :
You can use event delegation, to basically connect only one event handler to your entire document, and get the element which the event was originally dispatched, using 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.
}
编辑:您可以为所有人分配事件以这种方式编写一个简单的可拖动:
You can assign events to all your Scriptaculous draggables in this way:
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"
});
});
查看示例这里。
这篇关于使用onmousedown获取您刚才所拥有的元素的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文