简体   繁体   English

改变<input type="color">从单击到双击的行为

[英]Changing <input type= color> behaviour from single click to double click

By default clicking a will bring up a window that lets you pick a custom colour.默认情况下,单击 a 会弹出一个 window,让您可以选择自定义颜色。

I want to do this with a double click instead.我想通过双击来做到这一点。

I can stop single click from working with e.preventDefault but I can't work out how to make doubleClick do this behaviour instead.我可以停止单击使用 e.preventDefault 但我不知道如何让 doubleClick 改为执行此行为。

What I want:我想要的是:

handleDbClick = (e) => {
  //opens colour selection box
}
<input type="color" onDoubleClick={this.handleDbClick}/>

Solution:解决方案:

handleClick = (e) => {
  if(e.detail === 1) {
     e.preventDefault()
     //do stuff for one click
  }
  //open colour selection box
}

<input type="color" onClick={this.handleClick} />

Thanks谢谢

What about something like this:像这样的东西怎么样:

 const colorInput = document.getElementById("colorInput")
        let count = 0;
        let interval
        
        colorInput.addEventListener('click', e => {
            count++
            clearInterval(interval)
            interval = setInterval(() => {
             count = 0   
            }, 300)
            if(count === 2) {
                count = 0
            } else {

                e.preventDefault();
            }
        })

or this:或这个:

const colorInput = document.getElementById("colorInput")
        
        colorInput.addEventListener('click', e => {
            if(event.detail !== 2) {
                e.preventDefault()
            }
        })

You can simply use event.detail to check number of consecutive clicks, and use event.preventDefault() if they are not 2 .您可以简单地使用event.detail检查连续点击次数,如果不是2则使用event.preventDefault()

 let input = document.querySelector("#colorInput"); input.addEventListener("click", (e) => { if (e.detail.== 2) e;preventDefault(); });
 <input type="color" id="colorInput">

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

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