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

191
Views
obtener toda la línea de un área de texto y div editable

el clic derecho dentro de un área de texto da una línea completa, con respecto a la posición actual del cursor
Necesito lo mismo con un div editable, pero no funciona
la consola está vacía, es decir, hay una cadena vacía
por favor ayuda

 $(document).on('contextmenu', '#txstory', function(e){ e.preventDefault(); var borderChars = ['\n', '\r', '\t']; var tex = $(this).val(); var start = $(this)[0].selectionStart; var end = $(this)[0].selectionEnd; while (start > 0){ if(borderChars.indexOf(tex[start]) == -1){--start;} else{break;} } while(end < tex.length){ if(borderChars.indexOf(tex[end]) == -1){++end;} else{break;} } var str = tex.substr(start, end - start).trim(); console.log(str); }); $(document).on('contextmenu', '#ed', function(e){ e.preventDefault(); var borderChars = ['\n', '\r', '\t']; var tex = $(this).text(); var start = $(this)[0].selectionStart; var end = $(this)[0].selectionEnd; while (start > 0){ if(borderChars.indexOf(tex[start]) == -1){--start;} else{break;} } while(end < tex.length){ if(borderChars.indexOf(tex[end]) == -1){++end;} else{break;} } var str = tex.substr(start, end - start).trim(); console.log(str); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class='btx' id='txstory'>lorem ipsum</textarea> <div class='ed' id='ed' contenteditable>dolor sit</div>

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Obtenga el baseOffset de la selección del usuario, luego recorra cada línea y verifique si el desplazamiento está dentro del rango del índice del inicio de la línea y el índice del final de la línea:

 $(document).on('contextmenu', '#txstory', function(e){ e.preventDefault(); var borderChars = ['\n', '\r', '\t']; var tex = $(this).val(); var start = $(this)[0].selectionStart; var end = $(this)[0].selectionEnd; while (start > 0){ if(borderChars.indexOf(tex[start]) == -1){--start;} else{break;} } while(end < tex.length){ if(borderChars.indexOf(tex[end]) == -1){++end;} else{break;} } var str = tex.substr(start, end - start).trim(); console.log(str); }); $(document).on('contextmenu', '#ed', function(e){ var pos = document.getSelection().baseOffset; var lines = e.target.textContent.split("\n") var line; var characs = 0; for(let i = 0; i < lines.length; i++){ if(characs <= pos && characs + lines[i].length >= pos){ line = lines[i]; break; } characs += lines[i].length } console.log(line) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <textarea class='btx' id='txstory'>lorem ipsum</textarea> <div class='ed' id='ed' contenteditable>dolor sit</div>

about 4 years ago · Juan Pablo Isaza Report
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!