JS获取当前选中文字:
/**
* @description: 获取当前选择的文字
* @return String
*/
getSelected() {
if (window.getSelection) {
//ie9以上及其他主流浏览器
return window.getSelection().toString()
} else {
//ie9以下
const selection = document.selection?.createRange()
if (selection.text) {
return selection.text.toString()
}
return ''
}
},
JS 选中文字时不触发点击事件
点击事件触发时验证是否存在选中文字,有则返回,没有则继续
/**
* @description: 点击事件
* @return void
*/
numberClick() {
if (!!this.getSelected()) {
console.log('有选择的文字不触发点击事件,用户可能需要复制');
return
}
console.log('点击事件触发');
},
/**
* @description: 判断是否有选择的文字
* @return String
*/
getSelected() {
if (window.getSelection) {
//ie9以上及其他
return window.getSelection().toString()
} else {
//ie9以下
const selection = document.selection?.createRange()
if (selection.text) {
return selection.text.toString()
}
return ''
}
},