Foro de elhacker.net

Programación => Desarrollo Web => Mensaje iniciado por: RedZer en 20 Julio 2011, 23:29 pm



Título: problema al insertar texto en un area de texto
Publicado por: RedZer en 20 Julio 2011, 23:29 pm
bueno estoy realizando la opcion de poner texto en negritas,alinear ala izquierda,centrar, y justificar asi como las opciones k tiene el foro  todo eso funciona ala perfeccion el problema surge  cuando por ejemplo yo selecciono una palabra la cual deceo poner en negritas y pulso el icono de negritas el codigo me lo inserta el final ejemplo
si yo tengo seleccionado y deceo poner en negritas mi nombre RedZer al pulsar el icono de negritas me lo aroja al final asi
Código
  1. [b] [/b]
cuando lo que deberia de hacer es esto
Código
  1. [b]RedZer[/b]
anexo parte del  code que ocupo
este scrip es para que los codigos se inserten en el area de texto
Código
  1. <script language="javascript">
  2.      <!--  
  3.      function Smile(texto)
  4.  {
  5.      document.formulario.msj.value = document.formulario.msj.value + texto;
  6.      }
  7. </script>
  8.  

y este es el k ocupo para elegir las opciones
Código
  1. <a href=javascript:Smile('[b][/b]')><img src=../letras/bold.gif border=0 title="Negritas"></a>


Título: Re: problema al insertar texto en un area de texto
Publicado por: 4rkn63l en 24 Julio 2011, 06:43 am
Bueno a mi entender con tu codigo javascript estarias modificando todo el texto que se encuentre en el campo "msj" y no solamente el texto seleccionado, lo que necesitas es solo trabajar con esa parte (rango) de texto seleccionado, esto te ayudara http://www.disegnocentell.com.ar/notas2.php?id=206 (http://www.disegnocentell.com.ar/notas2.php?id=206)


Título: Re: problema al insertar texto en un area de texto
Publicado por: RedZer en 26 Julio 2011, 00:41 am
buscnado una solucion al problema encontre un code muy bueno la funcion que tiene es
hacer un editor de bbCode sencillo que admita selección de texto
y aki el code
Código
  1.  
  2. <title>Editor bbCode</title>
  3. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  4. function instag(tag){
  5. var input = document.form1.contenido;
  6. if(typeof document.selection != 'undefined' && document.selection) {
  7. var str = document.selection.createRange().text;
  8. input.focus();
  9. var sel = document.selection.createRange();
  10. sel.text = "[" + tag + "]" + str + "[/" +tag+ "]";
  11. sel.select();
  12. return;
  13. }
  14. else if(typeof input.selectionStart != 'undefined'){
  15. var start = input.selectionStart;
  16. var end = input.selectionEnd;
  17. var insText = input.value.substring(start, end);
  18. input.value = input.value.substr(0, start) + '['+tag+']' + insText + '[/'+tag+']'+ input.value.substr(end);
  19. input.focus();
  20. input.setSelectionRange(start+2+tag.length+insText.length+3+tag.length,start+2+tag.length+insText.length+3+tag.length);
  21. return;
  22. }
  23. else{
  24. input.value+=' ['+tag+']Reemplace este texto[/'+tag+']';
  25. return;
  26. }
  27. }
  28. function inslink(){
  29. var input = document.form1.contenido;
  30. if(typeof document.selection != 'undefined' && document.selection) {
  31. var str = document.selection.createRange().text;
  32. input.focus();
  33. var my_link = prompt("Enter URL:","http://");
  34. if (my_link != null) {
  35. if(str.length==0){
  36. str=my_link;
  37. }
  38. var sel = document.selection.createRange();
  39. sel.text = "[a href=\"" + my_link + "\"]" + str + "[/a]";
  40. sel.select();
  41. }
  42. return;
  43. }else if(typeof input.selectionStart != 'undefined'){
  44. var start = input.selectionStart;
  45. var end = input.selectionEnd;
  46. var insText = input.value.substring(start, end);
  47. var my_link = prompt("Enter URL:","http://");
  48. if (my_link != null) {
  49. if(insText.length==0){
  50. insText=my_link;
  51. }
  52. input.value = input.value.substr(0, start) +"[a href=\"" + my_link +"\"]" + insText + "[/a]"+ input.value.substr(end);
  53. input.focus();
  54. input.setSelectionRange(start+11+my_link.length+insText.length+4,start+11+my_link.length+insText.length+4);
  55. }
  56. return;
  57. }else{
  58. var my_link = prompt("Ingresar URL:","http://");
  59. var my_text = prompt("Ingresar el texto del link:","");
  60. input.value+=" [a href=\"" + my_link + "\"]" + my_text + "[/a]";
  61. return;
  62. }
  63. }
  64. </head>
  65.  
  66. <form name="form1" method="post" action="">
  67. <input type="button" name="Submit" value="B" onClick="instag('b')">
  68. <input type="button" name="Submit3" value="U" onClick="instag('u')">
  69. <input type="button" name="Submit4" value=" I " onClick="instag('i')">
  70. <input type="button" name="Submit2" value="LINK" onClick="inslink()">
  71. <br>
  72. <textarea name="contenido" cols="40" rows="10" id="contenido"></textarea>
  73.  
  74. </form>
  75. </body>
  76. </html>
  77.  
  78.