Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

104
Views
¿Cómo mover el cursor al nodo de texto y hacerlo editable si no hay contenido preexistente usando objetos de rango y selección?

Estoy tratando de hacer cambios de texto en función de cuándo el usuario hace clic en el botón "negrita" usando los objetos de rango y selección. Cuando el nodo de texto está vacío, el rango que configuré usando setStart() y setEnd() parece no funcionar porque el cursor no se mueve hacia el nodo de texto, sino que permanece en su nodo de texto inicial que ya está dentro mi div contenteditable . Sin embargo, si agrego algún contenido preexistente al nodo de texto, el rango es correcto, el cursor se mueve al nodo de texto recién creado y el contenido está en negrita al igual que mi entrada adicional. Entonces, ¿cómo hago para que el cursor entre en el nodo de texto sin ningún contenido? Gracias.

Aquí hay un fragmento de código:

 function setBold() { let parent = document.querySelector('.current'); /*contenteditable parent element*/ let sel = window.getSelection(); let selRange = s.getRangeAt(0); let bold = document.createElement('b'); let range = document.createRange(); /*if the node has content this solution works, but in this example it does not currently*/ let node = document.createTextNode(""); parent.appendChild(node); range.setStart(node,0); range.setEnd(node,node.length); range.surroundContents(bold); sel.removeAllRanges(); sel.addRange(range); }
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!