简体   繁体   English

按住按钮并更改查询选择器

[英]Press and hold button and change query selector

based on the question press and hold button javascript and the fiddle http://jsfiddle.net/0bs3omjj/1/ i want to change the code to another scenario. 基于问题,按住javascript按钮和小提琴http://jsfiddle.net/0bs3omjj/1/,我想将代码更改为其他方案。 I want to check if button1 is onmousedown - hold - onmouseup and if so... the query selector should change to the next button 我想检查button1是否为onmousedown-按住-onmouseup,如果是,则查询选择器应更改为下一个按钮

I tried this 我试过了

<div id="myDIV">
<button class="myButton">button 1</button>
<button class="myButton">button 2</button>
<button class="myButton">button 3</button>
<button class="myButton">button 4</button>
</div>

And JavaScript 和JavaScript

var mouse_is_down = false;
var current_i = 0;    
var buttoncounter = 1;
var button = "";
var buttoncount = 0;

function changebutton() {
 var x = document.getElementById("myDIV").querySelectorAll(".myButton");
 x[buttoncount].style.backgroundColor = "red";
 button = x[buttoncount];
 buttoncount++;
}

changebutton();

button.onmousedown = function(){
 mouse_is_down = true;
 console.log("mousedown" + buttoncounter);

setTimeout(
    (function(index){
        return function(){
            if(mouse_is_down && current_i === index){
                //do thing when hold
                console.log("hold" + buttoncounter);
                console.log(button);

            }
        };
    })(++current_i), 500); // time you want to hold before fire action
};

button.onmouseup = function(){
 mouse_is_down = false;
 current_i++;

 console.log("onmouseup" + buttoncounter);

 console.log("change selector");
 buttoncounter++;
 changebutton();
 console.log(button); 

};

But the query selector responds only on the first button (click & hold) - but the javascript changes the color values from the other buttons. 但是查询选择器仅对第一个按钮做出响应(单击并按住)-但是javascript会更改其他按钮的颜色值。 What is wrong? 怎么了?

First of all, don't use the same ID multiple times. 首先,请勿多次使用相同的ID。

Second, you're on the right path, but simply put the onmouseup and other calls within your changebutton() function: 其次,您走在正确的道路上,只需将onmouseup和其他调用放在您的changebutton()函数中:

Your HTML: 您的HTML:

<div id="myDIV">
  <button class="myButton">button 1</button>
  <button class="myButton">button 2</button>
  <button class="myButton">button 3</button>
  <button class="myButton">button 4</button>
</div>

Your Javascript: 您的Javascript:

var mouse_is_down = false;
var current_i = 0;    
var buttoncounter = 1;
var button = "";
var buttoncount = 0;

function changebutton() {
  var x = document.getElementById("myDIV").querySelectorAll(".myButton");
  x[buttoncount].style.backgroundColor = "red";
  button = x[buttoncount];
  button.onmousedown = function(){
  mouse_is_down = true;

  setTimeout(
    (function(index){
      return function(){
        if(mouse_is_down && current_i === index){
          //do thing when hold
          console.log("hold" + buttoncounter);
          console.log(button);

        }
      };
    })(++current_i), 500); // time you want to hold before fire action
  };

  button.onmouseup = function(){
    mouse_is_down = false;
    current_i++;
    buttoncounter++;
    changebutton();
  };
  buttoncount++;
}

changebutton();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM