【发布时间】:2018-08-10 02:54:28
【问题描述】:
每当用户完成选择时(我认为基本上是在每个selectstart 事件之后的第一个mouseup 事件上,我想),我都希望做一些事情。我想采用该选择并将其包装在要通过 CSS 设置样式的元素中。我认为 Selection API 为此提供了一个事件;但是,它似乎没有。
我不只是听mouseup,因为我特别希望它能够与浏览器的查找功能产生的选择一起使用(“在此页面中查找...”;⌘+f).
let selContainer = document.createElement('span')
span.classList.add('user-selection')
const wrapSelection = () => {
window.getSelection().getRangeAt(0).surroundContent(selContainer)
}
/* ┏━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃ ┃
┃ The Selection API only ┃
┃ affords these events: ┃
┃ ┃
┃ - selectionchange ┃
┃ - selectstart ┏━━━━━┫
┃ ┃issue┃
┗━━━━━━━━━━━━━━━━━━━━┻━━━━━┛
*/document.addEventListener('selectfinish', wrapSelection)/*
┗━━━━┳━━━━━┛
┃
┃
no such
event */
【问题讨论】:
标签: javascript event-handling observable dom-events textselection