首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查ul中的任何文本是否被选中

检查ul中的任何文本是否被选中
EN

Stack Overflow用户
提问于 2016-09-02 14:49:17
回答 5查看 442关注 0票数 3

我正在编写日志消息框/与聊天功能相同的功能,一旦有人在聊天中选择文本(例如,如果用户想粘贴某个内容),我希望将我的AutoScrollDown布尔值设置为false。

我使用ul和lis实现了日志消息/聊天框,如下所示:

代码语言:javascript
复制
<ul id="1" class="logbox">
   <li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
   <li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
   <li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
   <li>blabla</li>
</ul>

我尝试了以下事件来检查我的ul中的内容是否被选中,这是不起作用的:

代码语言:javascript
复制
  $("#1").on("focus", function() {
    autoScrollEnabled = 0;
  });

  $("#1").off("focus", function() {
    autoScrollEnabled = 1;
  });

我的问题:如果有人选择/高亮显示我ul中的文本,我如何将autoScrollEnabled设置为0,以及如何检查用户是否停止了选择/高举任何文本。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2016-09-07 16:02:35

要检测用户的文本是否高亮显示在日志框中,您应该首先侦听mousedown事件,然后查看鼠标仍然按下时是否存在mousemove事件。您可以在鼠标向下或鼠标移动时将您的标志设置为false,然后在mouseup事件中重置它。

代码语言:javascript
复制
document.querySelector('#one').addEventListener('mousedown', function (event) {

  window.onmousemove = function (event) {
    console.log('the user is selecting text');
  }
  
  window.onmouseup = function (event) {
    console.log('the user mousedup');
    console.log(window.getSelection());
    autoScrollEnabled = 1;
    window.onmousemove = null;
    window.onmouseup = null;
  }
  
  console.log('user mouseddown in ' + event.target.nodeName);
  autoScrollEnabled = 0;
}, false);
代码语言:javascript
复制
<ul id="one" class="logbox">
   <li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
   <li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
   <li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
   <li>blabla</li>
</ul>

票数 0
EN

Stack Overflow用户

发布于 2016-09-05 02:21:57

这个片段演示了:

它可以检测有或没有表单输入的文本的选择。 将触发mouseup事件,并充分处理误报,如单击文本,但没有突出显示。

  1. 委托整个文档侦听mouseup事件。
  2. 接下来,使用以下方法定义布尔状态: a.条件1(必须为真):存在选择对象 *只要任何文本是event.target (即点击、鼠标操作、放屁等),选择都是正确的。

b.条件2(不需要折叠):选择不只是对文本的一次单击(即没有突出显示)

*通过使用属性.isCollapsed,我们知道如果选择的起点与选择的结束点相同,则.isCollapsed为真,因此没有突出显示文本。

  1. 由于autoscroll从未被解释过,所以我将其简化为警报。 如果禁用了autoScroll,则警报还将包含选定的文本。

片段

代码语言:javascript
复制
/*
This Snippet demonstrates that:
   A. It can detect the selection of text with or without form inputs.
   B. Will trigger on mouseup event and adequately handle false positives like text clicked but nothing highlighted.
*/

// Reference a selection object
var selection = window.getSelection();

/*
1. Delegate the whole document to listen for a mouseup event.

2. Next define the boolean state with:

  a. Condition 1 (needs to be true): That a selection object exists
  
     * selection is true as soon as any text is the event.target (i.e. clicked, mouseup, farted on, etc.)
  b. Condition 2 (needs to be not collapsed): That the selection is not just a single click on text (i.e. nothing highlighted)
  
     * By using the property .isCollapsed we know that if the starting point of a selection is the same as the ending point of a selection, then .isCollapsed is true and therefore no text was highlighted.
     
3. Since autoscroll was never explained, I have simplified it as an alert.

   a. If autoScroll is disabled, the alert will also contain the text that's selected. 
*/

$(document).on('mouseup', function(e) {
  var state = (selection && !selection.isCollapsed) ? alert('autoScroll is disabled {{' + selection + '}} is selected') : alert('autoScroll is still enabled');
  return state;
});
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<ul id="list1" class="logbox">
  <li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
  <li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
  <li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
  <li>blabla</li>
</ul>

参考文献

代码语言:javascript
复制
- [window.getSelection()](https://developer.mozilla.org/en-US/docs/Web/API/Window/getSelection) 
- [.isCollapsed](https://developer.mozilla.org/en-US/docs/Web/API/Selection/isCollapsed)

票数 0
EN

Stack Overflow用户

发布于 2016-09-05 02:42:19

li元素没有focus事件,但是可以绕过它。看下面的片段。

代码语言:javascript
复制
var autoScrollOn = 1; //declare globally
var timeoutHandle = 0; //for additional service
$('#ul1').on('click', 'li', function() { //delegate click event
  clearTimeout(timeoutHandle); //clear timeout if any
  if ($(this).hasClass('selected')) { 
    //remove selection on the second click
    $('#ul1 li').removeClass('selected');
    autoScrollOn = 0;
  } else {
    //remove selection from siblings
    $('#ul1 li').removeClass('selected');
    //and add to the clicked element
    $(this).addClass('selected');
    autoScrollOn = 1;
    //restore autoscroll after 10s
    timeoutHandle = setTimeout(function() {
      $('#ul1 li').removeClass('selected');
      autoScrollOn = 0;
      console.log(autoScrollOn);
    }, 10000);
  }
  //do whatever you want with autoscroll
  console.log(autoScrollOn);
});
代码语言:javascript
复制
.debug {
  background: #aaf;
}
.error {
  background: #faa;
}
.warning {
  background: #ffa;
}
.selected {
  border: solid 1px #f00;
}
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id="ul1" class="logbox">
  <li class="debug"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.learn how to make a website. We offer free tutorials in all web development technologies.</li>
  <li class="error"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
  <li class="warning"><b>At</b> w3schools.com you will learn how to make a website. We offer free tutorials in all web development technologies.</li>
  <li>blabla</li>
</ul>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39295256

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档