บทความนี้อธิบายวิธีการรับข้อความที่เลือกซึ่งเข้ากันได้กับเบราว์เซอร์ต่างๆ แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
ภาพหน้าจอของเอฟเฟกต์การทำงานมีดังนี้:
รหัสเฉพาะมีดังนี้:
<! doctype html public "-// w3c // dtd html 4.01 // en" "http://www.w3.org/tr/html4/strict.dtd"> <html> <head> เลือก </title> <style type = "text/css"> #showselected {margin: 100px auto; ความกว้าง: 100%; ความสูง: 200px; ความเป็นมา: #ececec; ชายแดน: 1px Solid #CCC; } </style> <script type = "text/javascript"> // ส่งผ่านในวัตถุเพื่อรับฟังก์ชั่นข้อความที่เลือก getSelectedText (e) {// รับข้อความการเลือกภายใต้ IE if (document.selection) {return document.selection.createrange () ข้อความ; } // รับข้อความการเลือกภายใต้ firefox else ถ้า (window.getSelection (). toString ()) {return window.getSelection (). toString (); } // รับข้อความการเลือกของฟิลด์อินพุตหรือ textarea ภายใต้ firefox else ถ้า (E.SELECTIONSTART! = undefined && E.SelectionEnd! = undefined) {var start = eSelectionStart; var end = E.SelectionEnd; return e.value.substring (เริ่มต้นจบ); }} document.onmouseup = function () {var ta = document.getElementById ("mydiv"); document.getElementById ("showselected"). innerhtml = getSelectedText (document.body);} </script> </head> <body> <div id = "mydiv"> นี่คือโปรแกรมสำหรับการทดสอบ! </div> <div id = "showselected"> </div> </body> </html>สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเนื้อหาที่เกี่ยวข้องกับ JavaScript โปรดตรวจสอบหัวข้อของไซต์นี้: "สรุปเอฟเฟกต์และเทคนิคการสลับ JavaScript", "สรุปทักษะอัลกอริธึมการค้นหา JavaScript", "สรุปผลการสรุปและเทคนิคการสรุปของ JavaScript ของ JavaScript อัลกอริทึมและเทคนิคการสำรวจ JavaScript "และ" สรุปการใช้งานทางคณิตศาสตร์ JavaScript "
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม JavaScript ของทุกคน