拖放javascript查询

时间:2011-04-17 10:32:13

标签: javascript-events

<script type="text/javascript">
    //mouse down on dragged DIV element
    var flag = 0;

    function startdrag(t, e) {
    if (e.preventDefault) e.preventDefault(); //line for IE compatibility  
        e.cancelBubble = true;
        t.style.position = "absolute";
        window.document.onmousemove = dodrag;
        document.getElementById("resulttable").onmouseup = end;
        window.document.onmouseup = stopdrag;
        window.document.s = t;
        return false;
    }
    //move the DIV  
    function dodrag(e) {
        //alert("dodrag");
        if (!e) e = event; //line for IE compatibility

        t = window.document.s;
        if (t != null) {
            t.style.left = (t.offsetLeft + e.clientX - t.dragX) + "px";
            posleft = (t.offsetLeft + e.clientX - t.dragX) + "px";
            t.style.top = (t.offsetTop + e.clientY - t.dragY) + "px";
            t.dragX = e.clientX;
            t.dragY = e.clientY;
        }
        return false;
    }
    //restore event-handlers
    function stopdrag(e) {
        if (flag == 1)
            window.document.s = null;
        else
            alert("outside valid range"); // code for setting object to original area
        return false;
    }
    function end(e) {
        flag = 1;
        return false;      
    }
</script>

//  table  from where we start dragging
<td id="<%= i %>"><div onmousedown="startdrag(this, event);"> <%Response.Write(row1[i].ToString());  %></div> </td>


// target div where to put element
<div id="resulttable" onmouseup="end(event);" >Put whatever you want in here<br />
      <br />
      <br />
      <br />
    </div>

问题:

  1. 当我开始拖动时,会调用startdrag()的所有语句,然后调用dodrag(),依此类推。
  2. 虽然我们将元素放在目标div上,但
  3. end()并未被调用。所以旗帜仍为0。

1 个答案:

答案 0 :(得分:0)

根据我的评论,如果你可以使用Jquery UI和Jquery,因为它将完成所有繁重的工作。

关于你的问题,尝试使用end()stopdrag(),javascript可以在不同的浏览器中工作,检查另一个浏览器以查看调用是否真的有效... console.debug(“方法调用”);是一个很好的命令添加到您的代码,以帮助调试...