Sin más preámbulos, veamos el código
<! Doctype html public "-// w3c // dtd xhtml 1.0 transitional // en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transicional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml"> <fead> <meta http-equiv = "content-type" content = "text/html; charset = utf-8"/> <title> JS Método de escritura que excede el texto con Ellipses </title> </body> <body> {// La longitud del carácter chino leído por el atributo de longitud es 1 if (str.length*2 <= len) {return str; } var strlen = 0; var s = ""; for (var i = 0; i <str.length; i ++) {s = s+str.charat (i); if (str.charcodeat (i)> 128) {strlen = strlen + 2; if (strlen> = len) {return s.substring (0, s.length-1) + "..."; }} else {strlen = strlen + 1; if (strlen> = len) {return s.substring (0, s.length-1) + "..."; }} else {strlen = strlen + 1; if (strlen> = len) {return s.substring (0, s.length-2) + "..."; }}} return s;} window.Onload = function () {var str = document.getElementById ('Cut_str'). InnerHtml; var s = Cutstring (Str, 15); document.getElementById ('CUT_STR'). InnerHtml = s;} </script> <div> <p id = "CUT_STR"> El método de escritura JS que excede el texto se reemplaza por elipses. </p> </div> </body> </html>Renderizaciones de implementación
Resumir
De hecho, lo principal es usar el método substring de JS. Todavía es muy simple implementar esta función, pero es muy práctico. Lo anterior es todo el contenido de este artículo. Espero que este artículo sea de ayuda para todos.