我有一个功能(仍在开发中),需要在 div 中捕获用户选择的文本(不是文本区域和输入框,因此 .select 不起作用。)。Using .click and selection and range objects works great when the selection is contained within a single element in that div but fails to trigger if the user selection spans multiple elements within the div (like multiple p's).
$('.relevantDivClass').click(function(){
console.log('Got here');
SelObj = window.getSelection();
Range = SelObj.getRangeAt(0);
if (SelObj.anchorNode == SelObj.focusNode){
console.log('executing same node branch');
string = SelObj.toString();
console.log(string)
}
else{
console.log('executing multiple node branch');
//code...
}
});
关于如何在这种情况下触发该事件的任何想法?它仍然在relatedDivClass 中,所以我对为什么.click 不会触发感到困惑。
提前致谢!