廢話不說,直接上代碼:html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <script> document.onmouseup = document.ondbclick= selceText; function selceText(){ var txt; if(document.selection){ txt = document.selection.createRange().text }else{ txt = window.getSelection()+''; } if(txt){ alert(txt); } } </script> </head> <body> <p>哈哈哈呵呵呵呵嘿嘿</p> </body> </html>
頁面中的文字,經過選中後自動彈出選擇的文字!code