Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果第一个输入是集中的,则将其他输入写入第二个输入_Javascript_Jquery_Html - Fatal编程技术网

Javascript 如果第一个输入是集中的,则将其他输入写入第二个输入

Javascript 如果第一个输入是集中的,则将其他输入写入第二个输入,javascript,jquery,html,Javascript,Jquery,Html,例如,如果第一次输入.res是聚焦的,我如何使其在上写入其他内容。res2我首先尝试了这个。res是聚焦的,然后当您单击进入的按钮时。operatorsdiv focus移动到第二次输入。res2但它不起作用 $(".one,.two,.three,.four,.five,.six,.seven,.eight,.nine,.zero,.dot").click(function(){ if($('.res').is(":focus")){ f

例如,如果第一次输入
.res
是聚焦的,我如何使其在
上写入其他内容。res2
我首先尝试了这个
。res
是聚焦的,然后当您单击进入
的按钮时。operators
div focus移动到第二次输入
。res2
但它不起作用

$(".one,.two,.three,.four,.five,.six,.seven,.eight,.nine,.zero,.dot").click(function(){
            if($('.res').is(":focus")){
                firstNumber = $('.res').val(($('.res').val()) + $(this).val());
            } else {
                secondNumber = $('.res2').val(($('.res2').val()) + $(this).val());
            }
        })
$(".plus,.subtract,.divide,.multiply").click(function(){
            resultWindow.blur();
            $('.res2').focus();
        })

1.
2.
3.

4. 5. 6.
7. 8. 9
0 . + - / * =
主要问题是,每次单击按钮时,输入模糊,按钮获得焦点

因此,
$('.res').is(“:focus”)
将始终返回
false

要实现这一点,您需要添加另一个事件侦听器以防止按钮单击的默认行为:

$("button").mousedown(function(e) {e.preventDefault()})
此外,我建议使用此查询选择器:

$(".numbers > button")
$(".operators > button")
因此,您的代码看起来更简单

看看它是如何工作的:

$(“按钮”).mousedown(函数(e){e.preventDefault()})
$(“.numbers>按钮”)。单击(函数(){
如果($('.res').is(“:focus”)){
firstNumber=$('.res').val($('.res').val())+$(this.val());
}否则{
secondNumber=$('.res2').val($('.res2').val())+$(this.val());
}
})
$(“.operators>按钮”)。单击(函数(){
resultWindow.blur();
$('.res2').focus();
})

1.
2.
3.

4. 5. 6.
7. 8. 9
0 . + - / * =
尝试在按钮上执行鼠标向下事件而不是单击事件,然后防止默认设置,这样可以防止输入失去焦点

$(".one,.two,.three,.four,.five,.six,.seven,.eight,.nine,.zero,.dot").on('mousedown', function(event){
  event.preventDefault();
  if($('.res').is(":focus")){...}
  ...
});

有什么解决方案吗?若你们在一个并没有“鼠标”的移动浏览器上运行这个会发生什么?大多数移动浏览器都会通过触摸来抛出点击事件,但你们也可以使用
。on('mousedown touchtstart',function(event){…})