gpt4 book ai didi

javascript - 在 HTML 中拖放

转载 作者:行者123 更新时间:2023-11-28 16:32:23 27 4
gpt4 key购买 nike

你好,我正在写一个小计算器来练习。目前还没有逻辑。我仍然希望允许用户在浏览器中拖动计算器。

因此,我将计算器元素嵌套在这个标签中,并实现了一些拖放逻辑(主要来自教程)。但还是不行。

有人看到我做错了什么吗?

    <!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 in Orbit ;)

关于javascript - 在 HTML 中拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5470119/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com