jQuery复制到剪贴板,然后在几秒钟后撤消

时间:2018-09-17 21:34:01

标签: javascript jquery

我具有以下功能,可将div中的文本复制到剪贴板。 3秒后,我希望它取消选择文本。我无法通过setTimeout取消选择div中的文本。我尝试了模糊,触发和单击。有解决办法吗?

function copyUsingJquery(element_id) {
        $(element_id).attr("contenteditable", true)
          .select()
          .on("focus", function () {
              document.execCommand('selectAll', false, null)
          })
          .focus();
        document.execCommand("Copy");
        $(element_id).removeAttr("contenteditable");

        setTimeout(function () {
            //deslect text on page after a few seconds
            $(element_id).trigger("click");
        }, 3000);

    }

2 个答案:

答案 0 :(得分:0)

工作解决方案:

ind <- 500000
individuals <- as.character(seq(1, ind, by = 1))
maxCol <- 7
col <- 4
line <- 0
a <- 0
b <- 0
c <- 0

col_array <- c("year","time", "ID", "age", as.vector(outer(c(paste(seq(0, 1, by = 1), "year", sep="_"), paste(seq(2, maxCol, by = 1), "years", sep="_")), c("S_F", "I_F", "R_F"), paste, sep="_")))
array1 <- array(sample(1:100, length(col_array), replace = T), dim=c(2, length(col_array), ind), dimnames=list(NULL, col_array, individuals)) ## 3rd dimension = individual ID

dim(array1)
# [1]      2     28 500000    # Two rows x 28 measures x 500k individuals

答案 1 :(得分:0)

您不需要jQuery。这是您jQuery代码的直接翻译。

const unselect = () => window.getSelection().removeAllRanges()

function copyWithoutJQuery(elementId) {
  const el = document.getElementById(elementId)
  el.setAttribute('contenteditable', true)
  el.addEventListener('focus', () => {
    document.execCommand('selectAll', false, null);
    document.execCommand('Copy');
    el.removeAttribute('contenteditable');
    setTimeout(unselect, 3000);
  })

  el.focus();

  const range = document.createRange();
        range.selectNodeContents(el);
  const selection = window.getSelection();
        selection.removeAllRanges();
        selection.addRange(range);
}