簡體   English   中英

Javascript:長按文件對話框

[英]Javascript: Long Press for File Dialog

是否可以長按文件對話框? 例如JavaScript中的長按? 有長期按下觸發事件的答案。 但是,這不能用於在大多數瀏覽器中觸發文件輸入單擊,因為它不被視為用戶激活。

var pressTimer;

$("a").mouseup(function(){
  clearTimeout(pressTimer);
  // Clear timeout
  return false;
}).mousedown(function(){
  // Set timeout
  pressTimer = window.setTimeout(function() 
  {    fileChooser.click() // assume fileChoose is a file input element
       // This is suppressed by most browsers.
  },1000);
  return false; 
});

當然有可能。 您可能必須使用mousedownmouseup事件為它開發自己的檢測。 計算mousedownmouseup之間的時間,並確定延遲是否足夠長以引起其他一些操作。

https://jsfiddle.net/psc4yk76/2/

(function() {
  const longtime = 500;
  const target = document.getElementById('target');
  const input = document.getElementById('input');
  const button = document.getElementById('button');

  var timedown = 0;

  button.onmousedown = () => {
    timedown = new Date().getTime();
  };

  button.onmouseup = () => {
    let timeup = new Date().getTime();
    let insert = document.createElement('div');

    if( (timeup - timedown) < longtime ) {
      insert.appendChild(document.createTextNode('Short!'));
    } else {
      insert.appendChild(document.createTextNode('Long!'));
      input.click();
    }

    target.appendChild(insert);
  }
})()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM