【问题标题】:How to click on an element, move the mouse over other elements and then select those elements that you hovered over with JavaScript?如何单击一个元素,将鼠标移到其他元素上,然后选择您使用 JavaScript 悬停的那些元素?
【发布时间】:2020-10-03 08:18:27
【问题描述】:

我基本上是在尝试复制jQuery.selectable() 方法,但我需要在vanilla JavaScript 中使用它。我应该使用哪种event listener 方法?我已经尝试使用mousedown,但我如何检测到另一个元素已被访问? 以下是jQuery 的示例:[http://jsfiddle.net/ZfevM/99/]

【问题讨论】:

    标签: javascript events mouseevent event-listener jquery-ui-selectable


    【解决方案1】:

    你可以使用 window.getSelection() 和 [selection].getRangeAt([index]);

    document
      .getElementsByTagName('body')[0]
      .addEventListener('mouseup', 
         function(){ 
           const selection = window.getSelection(); 
           const range = selection.getRangeAt(0); 
    
           console.log( selection, range ) 
      })
    

    Selection 具有 .containsNode() 函数来与给定集合进行比较; Range 具有 .startContainer 和 .endContainer 以及 .commonAncestorContainer 以与给定结构进行比较

    【讨论】:

      【解决方案2】:

      我能够弄清楚。还必须与mouseupmousemove 一起工作。这是我想出的,它按预期工作:[https://repl.it/repls/EquatorialLumberingInfinity]

      欢迎任何想法或替代解决方案!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-22
        • 2022-12-05
        • 2013-02-05
        • 2016-08-03
        • 1970-01-01
        • 1970-01-01
        • 2015-11-18
        • 2021-01-13
        相关资源
        最近更新 更多