[英]while-mousedown
I need to solve this problem. 我需要解决这个问题。
When the mouse button is pressed down on the max button the variable max
should increase by one. 当在最大按钮上按下鼠标按钮时,变量max
应该增加一。 When I hold down the min button the max
variable should decrease but it's not working for me. 当我按下最小按钮时, max
变量应减小,但对我不起作用。
How do I get this to work? 我该如何工作?
<body>
<button id="max" onmousedown="max()" >max</button>
<input id="num" type="number" disabled="disabled" value="0">
<button id="min" onmousedown="min()">min</button>
<script type="text/javascript">
function max()
{
var x = document.getElementById("num").value
if (x<100) {var max= document.getElementById("num").value++;}
}
function min()
{
var x = document.getElementById("num").value
if (x>0) {var max= document.getElementById("num").value--;}
}
</script>
</body>
This should work. 这应该工作。
function max()
{
var numElement = document.getElementById("num");
var x= numElement.value
var fnc = function ()
{
if (x<100){
document.getElementById("num").value = x;
x++;
}
var i = setInterval(fnc, 100);
fnc();
document.onmouseup = function ()
{
clearInterval(i);
}
}
function min()
{
var numElement = document.getElementById("num");
var x= numElement.value
var fnc = function ()
{
if (x>0){
document.getElementById("num").value = x;
x--;
}
var i = setInterval(fnc, 100);
fnc();
document.onmouseup = function ()
{
clearInterval(i);
}
}
A few things: 一些东西:
1 - I've removed the inline events from your input tags. 1-我从您的输入代码中删除了内联事件。
2 - In order to get the intervals to work correctly you need to have a reference to that process. 2-为了使间隔正常工作,您需要参考该过程。 In my example minInterval
and maxInterval
allow me to call clearInterval
onmouseup
. 在我的示例中, minInterval
和maxInterval
允许我调用clearInterval
onmouseup
。
3 - For these to work correctly you'll need to run the script after the DOM is loaded/ready, so put the code near the end of your body
tag. 3-为了使它们正常工作,您需要在DOM加载/就绪后运行脚本,因此请将代码放在body
标签末尾。
4 - The value of speed
is basically how quickly you want the intervals to be ran. 4- speed
值基本上是您希望间隔运行的速度。
HTML 的HTML
<button id="max" >max</button>
<input id="num" type="number" disabled="disabled" value="0">
<button id="min">min</button>
JavaScript 的JavaScript
var min = document.getElementById("min"),
max = document.getElementById("max"),
num = document.getElementById("num"),
speed = 10,
minInterval, maxInterval;
min.onmousedown = function(){
minInterval = setInterval(function(){
num.value--;
}, speed);
};
min.onmouseup = function(){
clearInterval(minInterval);
};
max.onmousedown = function(){
maxInterval = setInterval(function(){
num.value++;
}, speed);
};
max.onmouseup = function(){
clearInterval(maxInterval);
};
Helpful links: 有用的网址:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.