Tengo un archivo HTML5 y estoy tratando de crear una aplicación como Google Docs usando create-electron-app.
Tengo una etiqueta de área de texto para la entrada y estoy tratando de poner en negrita el texto que el usuario ha seleccionado. Tengo la función getSelectedText() funcionando y está devolviendo una cadena como se esperaba, pero necesito poner ese texto en negrita dentro del área de texto. Si ha utilizado Google Docs, debe saber a qué me refiero.
Aquí está el código:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> <title>Make word documents!</title> <link rel="stylesheet" href="index.css" /> </head> <body> <textarea class="userInput"></textarea> <input type="button" value="Bold" onmousedown="bold()"> <script> function getSelectedText() { if (window.getSelection) { return "" + window.getSelection(); } else if (document.selection && document.selection.type == "Text") { return document.selection.createRange().text; } return ""; } function bold() { var userInput = getSelectedText(); // userInput.style.fontWeight = "bold"; // print the type of the userinput variable console.log(typeof userInput); } </script> </body> </html>

Usuario1937363675 publicado
hola francisco
En realidad, creo que deberías usar "span" en lugar de "textarea", porque de acuerdo con algunos materiales, Web Editor casi usa span. Escribí un breve ejemplo para ti, ¡espero que pueda ayudarte!
--------------
<html>
<cabeza>
<lenguaje de script="javascript">
prueba de función (e)
{
mitexto.innerHTML="<b>"+mitexto.innerText+"</b>";
}
</script>
</cabeza>
<cuerpo>
<span contenteditable id="mytext" style="height:200;width:95%;border:1px solid black">
Editor HTML
</span>
<tipo de entrada="botón" valor="Negrita" onclick="prueba()" />
</cuerpo>
</html>