拖动;放入HTML

时间:2011-03-29 09:01:23

标签: javascript html drag-and-drop

你好,我正在写一个小计算器进行锻炼。目前还没有逻辑。我还是想让用户在浏览器周围拖动计算器。

所以我在这个-Tag中嵌套了我的Calculator元素,并实现了一些Drag& Drop-logic(主要来自教程)。它仍然不起作用。

有人看到我做错了吗?

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">


                    <title>myCalculator</title>

                    <script type="text/javascript">

                    var dragObject = null;
                    var dragX = 0;
                    var dragY = 0;

                    var posX = 0;
                    var posY = 0;

                    function handleInput(currentInput){

                        alert(currentInput);

                        var x = 0;                              

                        x = currentInput;

                    }

                    function saveOperator(currentOperator){

                        alert(currentOperator);

                    }

                    function dragInit(){

                        document.onmousemove = drag;
                        document.onmouseup = dragStop;

                    }

                    function dragStop(){
                        dragObject = null;
                    }

                    function dragStart(element){
                        dragObject = element;
                        dragX = posX - dragObject.offsetLeft;
                        dragY = posY - dragObject.offsetTop;
                    }

                    function drag(event){

                        posX = document.all ? window.event.clientX : event.pageX;
                        posY = document.all ? window.event.clientY : event.pageY;

                        if(dragObject != nul){
                            dragObject.style.left = (posX - dragX) + "px";
                            dragObject.style.top = (posY - dragY) + "px";
                        }
                    }


                    </script>

    </head>
            <body onload="dragInit">




                     <div id="calc" onmousedown="dragStart(this)" style="position:absolute;top:10px;left:10px;height:270px;width:200px;background:#006600"> 


                     <h1 id="headLine"><b>myCalculator</b></h1>


                    <input type="text" id="calcField"/>


                    <table style="position: absolute;" id="calculatorTable">

                    <tr>
                        <td><input id="7" type="button" value="7" onclick="handleInput(this.value)"/></td>
                        <td><input id="8" type="button" value="8" onclick="handleInput(this.value)"/></td>
                        <td><input id="9" type="button" value="9" onclick="handleInput(this.value)"/></td>
                        <td><input id="/" type="button" value="/" onclick="saveOperator(this.value)"/></td>
                    </tr>
                    <tr>
                        <td><input id="4" type="button" value="4" onclick="handleInput(this.value)"/></td>
                        <td><input id="5" type="button" value="5" onclick="handleInput(this.value)"/></td>
                        <td><input id="6" type="button" value="6" onclick="handleInput(this.value)"/></td>
                        <td><input id="*" type="button" value="*" onclick="saveOperator(this.value)"/></td>
                    </tr>
                    <tr>
                        <td><input id="1" type="button" value="4" onclick="handleInput(this.value)"/></td>
                        <td><input id="2" type="button" value="5" onclick="handleInput(this.value)"/></td>
                        <td><input id="3" type="button" value="6" onclick="handleInput(this.value)"/></td>
                        <td><input id="-" type="button" value="-" onclick="saveOperator(this.value)"/></td>
                    </tr>
                    <tr>
                        <td><input id="0" type="button" value="0" onclick="handleInput(this.value)"/></td>
                        <td><input id="," type="button" value="," onclick="handleInput(this.value)"/></td>
                        <td><input id="+" type="button" value="+" onclick="saveOperator(this.value)"/></td>
                        <td><input id="=" type="button" value="=" onclick="saveOperator(this.value)"/></td>
                    </tr>

                    </table>
                    </div>


            </body>     
</html>

1 个答案:

答案 0 :(得分:4)

第56行有语法错误。请使用null代替nul。您可能希望使用JS语法检查器,例如JSLint

然后,第66行,你可能想要调用dragInit函数,所以你应该用括号写<body onload="dragInit()">(不是括号,感谢Orbit中的Lightness Races;)