var str = ''
// 添加 mouseup 事件监听器
document.addEventListener('mouseup', handleMouseUp);
// 添加 selectionchange 事件监听器
document.addEventListener('selectionchange', handleSelectionChange);
// 处理 mouseup 事件的回调函数
function handleMouseUp(event) {
// 在这里处理鼠标选中文本事件结束的逻辑
if(str.length!==0){
console.log('鼠标选中文本事件结束',str);
str = ''
}
}
// 处理 selectionchange 事件的回调函数
function handleSelectionChange(event) {
// 检查选中的文本范围是否为空
const selection = window.getSelection();
if (selection && selection.toString().length != 0) {
// 在这里处理文本选中取消的逻辑
str = selection.toString()
}
}
JS选中文本以及鼠标抬起事件监听
最新推荐文章于 2024-12-01 20:46:56 发布