在mouseup之后获取Element

时间:2010-03-02 07:59:08

标签: javascript-events

我有一个html页面如下:

<table cellpadding="1" cellspacing="1" border="1">
        <tr>
            <td id="abc">
                <div style="height: 23px; overflow: hidden; position: relative; height: 70px; width: 70px;
                    background-color: red" valign="top" id="dragable3">
                    <a style="color: white" class="CalendarPlus-Item-2010311422" title="" href="#"><font
                        color="white">New Item</font></a></div>
            </td>
            <td>
                <div id="4" style="width: 80px; height: 80px;">
            </td>
            <td>
                <div id="5" style="width: 80px; height: 80px;">
            </td>
        </tr>
        <tr>
            <td>
                <div id="8" style="width: 80px; height: 80px;">
            </td>
            <td>
                <div id="7" style="width: 80px; height: 80px;">
            </td>
            <td>
                <div id="6" style="width: 80px; height: 80px;">
            </td>
        </tr>
    </table>

和jquery.js以及文件javascript:

var DragHandler = {

// private property.
_oElem: null,

// public method. Attach drag handler to an element.
attach: function(oElem) {
    oElem.onmousedown = DragHandler._dragBegin;
    return oElem;
},

// private method. Begin drag process.
_dragBegin: function(e) {
    var oElem = DragHandler._oElem = this;

    if (isNaN(parseInt(oElem.style.left))) { oElem.style.left = '0px'; }
    if (isNaN(parseInt(oElem.style.top))) { oElem.style.top = '0px'; }

    var x = parseInt(oElem.style.left);
    var y = parseInt(oElem.style.top);

    e = e ? e : window.event;
    oElem.mouseX = e.clientX;
    oElem.mouseY = e.clientY;

    document.onmousemove = DragHandler._drag;
    document.onmouseup = DragHandler._dragEnd;
    return false;
},

// private method. Drag (move) element.
_drag: function(e) {
    var oElem = DragHandler._oElem;

    var x = parseInt(oElem.style.left);
    var y = parseInt(oElem.style.top);

    e = e ? e : window.event;
    oElem.style.left = x + (e.clientX - oElem.mouseX) + 'px';
    oElem.style.top = y + (e.clientY - oElem.mouseY) + 'px';

    oElem.mouseX = e.clientX;
    oElem.mouseY = e.clientY;

    return false;
},

// private method. Stop drag process.
_dragEnd: function() {
    document.onmousemove = null;
    document.onmouseup = null;
    DragHandler._oElem = null;

},

_onPostBack: function(form, ehandle, arg) {
    var theForm = document.forms[form];
    if (!theForm) {
        theForm = document.aspnetForm;
    }
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = ehandle;
        theForm.__EVENTARGUMENT.value = arg;
        theForm.submit();
    }
}
}

DragHandler.attach(document.getElementById('dragable3'));

如何在移动的div(id =“dragable3”)下获取Element。示例:我将div标签(id =“dragable3”)从td标签(id = abc)移动到div标签(id =“6”)。如何在mouseup之后获得div标签(id =“6”)?

谢谢,

Phong Dang。

2 个答案:

答案 0 :(得分:0)

为div id = 6编写鼠标事件。使用onmouseup或onmouseover。在那里你可以捕捉鼠标事件,并做需要的东西。

答案 1 :(得分:-1)

我们无法为div(id = 6)设置onmouseover或onmouseup,因为此事件无法触发。