Este artículo describe el método para obtener un texto seleccionado que es compatible con varios navegadores. Compártelo para su referencia, como sigue:
La captura de pantalla del efecto de ejecución es la siguiente:
El código específico es el siguiente:
< Seleccionado </title> <style type = "text/css"> #showselected {margin: 100px auto; Ancho: 100%; Altura: 200px; Antecedentes: #ececec; borde: 1px sólido #ccc; } </style> <script type = "text/javaScript"> // Pase en el objeto para obtener la función de texto seleccionada GetSelectedText (e) {// Obtenga el texto de selección en IE if (document.selection) {return document.selection.createrGe (). Text; } // Obtenga el texto de selección en Firefox else if (window.getSelection (). ToString ()) {return Window.getSelection (). ToString (); } // Obtenga el texto de selección del campo Entrada o TextAREA en Firefox else if (E.SelectionStart! = Undefined && E.SelectionEd! = Undefined) {var start = e.selectionStart; var end = e.selectionend; return E.Value.Substring (inicio, finalización); }} document.onmouseUp = function () {var ta = document.getElementById ("myDiv"); document.getElementById ("showSelected"). innerHtml = getSelectedText (document.body);} </script> </head> <body> <div id = "myDiv"> ¡Este es un programa para las pruebas! </div> <div id = "showSelected"> </div> </body> </html>For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript Algoritmos y técnicas de recorrido ", y" Resumen del uso de operaciones matemáticas de JavaScript "
Espero que este artículo sea útil para la programación de JavaScript de todos.