嗨,我在写一个小计算器锻炼身体。目前还没有逻辑。我仍然希望允许用户在浏览器中拖动计算器。

因此,我将我的Calculator元素嵌套在此-Tag中,并实现了一些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>

最佳答案

您在第56行出现语法错误。使用null代替nul。您可能想要使用JS语法检查器,例如JSLint

然后,在第66行,您可能想调用dragInit函数,因此您应该在括号中写上<body onload="dragInit()">(而不是花括号,这要感谢“轨道中的Lightness Races”;)

10-06 00:09