这可能吗?
我正在尝试为 onmousedown 编写一个函数,它将返回您刚刚单击的元素的 ID,以便稍后在不同的 div 中重新创建该元素时使用。
您可以使用事件委托 http://www.sitepoint.com/blogs/2008/07/23/javascript-event-delegation-is-easier-than-you-think/,基本上只将一个事件处理程序连接到整个文档,并获取最初调度该事件的元素,使用事件目标 https://developer.mozilla.org/en/DOM/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.
}
Edit:您可以通过以下方式将事件分配给所有 Scriptaculous 拖动对象:
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"
});
});
检查一个例子here http://jsbin.com/ejure.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)