JS 获取鼠标选中的文字并处于选中状态

项目中需要实现这么个效果
选中字段
简而言之:鼠标选中一部分文字之后,停留在此状态,并添加前后标志
网上搜了没有此场景的实现方法,只能自己写了;

<div class="d-inline" ng-mousedown="cutMouseDown()"
	 ng-mouseup="cutMouseUp($event)" ng-bind-html="cutedSample"></div>
class CutDemo {
  constructor() {
    this.cutedSample = ''; // 等待被裁剪的字段
    this.selection = null; // 裁剪字段选中的section
    this.isParsed = false; // 是否已解析预览
    this.maxLenSample = 'bbb'; // 未被裁剪的原字段
  }
  cutMouseDown() {
    if (this.isParsed) return;
    this.cutedSample = this.maxLenSample; // 恢复成纯字符串, 等待被再次裁剪
  }

  cutMouseUp(event) {
    if (this.isParsed) return;
    this.selection = this.getSelectText(event.target);
  }

  getSelectText() {
    const selection = window.getSelection();
    if (selection.type === 'Range') {
      //Caret和null不是选中状态
      const start = Math.min(selection.anchorOffset, selection.focusOffset);
      const end = Math.max(selection.anchorOffset, selection.focusOffset);
      if (start === end) return null;
      const first = this.maxLenSample;
      const startHtml = first.substring(0, start);
      const middleHtml = first.substring(start, end);
      const endHtml = first.substring(end);
      const prefix = '<div class="selection-tag"><div class="st-dot"></div><div class="st-line"></div></div>'; // 标记前缀
      const suffix = '<div class="selection-tag"><div class="st-line"></div><div class="st-dot"></div></div>'; // 标记后缀
      this.cutedSample = startHtml + '<span class="selection-text">' + prefix + middleHtml + suffix + '</span>' + endHtml;
      return {
        text: selection.toString(),
        end,
        start,
        focusNode: selection.focusNode,
        anchorNode: selection.anchorNode,
        rangeCount: selection.rangeCount,
      };
    } else {
      return null;
    }
  }
}

样式:

.selection-text {
  background-color: fade(#4ad3a3, 20);
  position: relative;

  .selection-tag {
    position: absolute;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    width: 0;

    .st-dot {
      width: 5px;
      height: 5px;
      border-radius: 5px;
      background-color: #4ad3a3;
    }

    .st-line {
      width: 1px;
      height: 10px;
      background-color: #4ad3a3;
    }
  }
}

以上代码如有需要,可根据自己项目中的框架进行替换~

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取鼠标左键选中的内容,我们可以借助JavaScript来实现。可以通过监听鼠标按键的点击事件来判断是否按下了鼠标左键,并在鼠标拖动的过程中实时获取选中的内容。 首先,我们需要给需要选中内容的元素添加一个鼠标按下事件的监听器,当鼠标按下时,获取鼠标按下的位置坐标。 接着,在鼠标移动的过程中,我们就可以根据鼠标的位置坐标和选中的内容区域来获取选中文字内容。我们可以通过`window.getSelection()`来获取当前选中的内容。 最后,在鼠标抬起时,我们就可以得到最终选中的内容了。可以通过`window.getSelection().toString()`来获得选中文字内容。 以下是一个简单的示例代码: ```javascript // 获取选中的内容 function getSelectedText() { const selectedText = window.getSelection().toString(); console.log(selectedText); } // 监听鼠标按下事件,获取按下的位置坐标 document.addEventListener('mousedown', function(event) { if (event.button === 0) { // 判断是否是鼠标左键按下 var startPoint = { x: event.clientX, y: event.clientY }; document.addEventListener('mousemove', mouseMoveHandler); } }); // 监听鼠标抬起事件,获取选中的内容 document.addEventListener('mouseup', function(event) { if (event.button === 0) { // 判断是否是鼠标左键抬起 document.removeEventListener('mousemove', mouseMoveHandler); getSelectedText(); } }); // 鼠标拖动事件,实时获取选中的内容 function mouseMoveHandler(event) { var currentPoint = { x: event.clientX, y: event.clientY }; // 根据起始点和当前点坐标计算选中的内容区域 // 然后可以根据这个区域来获取选中的内容 } ``` 以上就是使用JavaScript获取鼠标左键选中的内容的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值