<!DOCTYPE html> <html lang="en"> <head></head> <body> <div><p onm ouseup="mytest()">只是一段文本其余的还有很多的文字</p></div> <script> function mytest(e){ console.log(window.getSelection); var txt = window.getSelection?window.getSelection():document.selection.createRange().text; if(txt==""){//当选中内容为空时,阻止事件发生 window.event? window.event.cancelBubble = true : e.stopPropagation(); }else{ var txt1=String.toString(txt);//得到的选中的文本是一个对象,需要转化为字符串 alert(txt1); } } </script> </body> </html>
标签:盒子,getSelection,获取,window,选中,var,txt,event From: https://www.cnblogs.com/Ma-YuHao/p/16710868.html