在按住 ctrl 键单击表格元素时删除默认蓝色边框



这个问题以前有人问过,但没有一个答案对我有用。

我尝试过的事情:

event.preventDefault() - 不执行任何操作。

从CSS中删除用户选择 - 不可行,用户应该仍然能够选择文本。

按 Ctrl+单击时从事件中删除选择 - 有延迟,仅在鼠标向上后有效

上面的例子:

document.body.querySelector('tbody').addEventListener('click', e => {
    e.preventDefault();
  if (e.target.tagName === 'TD') {
    if(e.ctrlKey){
        //Select row logic here
        if (document.selection){
        document.selection.empty();
      }else if (window.getSelection){
        window.getSelection().removeAllRanges();
      }
    }
  }
})

http://jsfiddle.net/ppgab/zm1dgt3s/5/

当用户单击"在"表格单元格之间,然后选择所有单元格时,这会变得格外烦人。

这个问题有什么优雅的解决方案吗?请不要使用JQuery

我只是使用了错误的事件,鼠标向下是正确的:

table.addEventListener('mousedown', e => {
    if(e.ctrlKey){
        e.preventDefault();
    }
})

你用CSS试试吗?

div#myDiv:active{
    text-decoration: none;
}

最新更新