简体   繁体   中英

Select text inside html table cell upon tabbing into the cell

I feel silly for asking this, it seems like it should be very simple, and yet after several hours - here I am.

I have an HTML Table with <td> cells inside. Each cell has a default of "0". I'm trying to figure out how to select the cell contents upon tabbing and/or clicking into the cell. I've tried using CSS (ie,. user-select: all; user-select: text) and the best I can do with javascript is to clear the cell on click:

document.getElementById(tableId).addEventListener('click', (e) => {
     if (!e.target.contentEditable === "true") return; //using this to register clicks on a cell
     e.target.innerText = ""
 })

I'd really like to just select the contents - not clear them - so the user can leave the value there and tab to the next one if they want to keep the default value. I've tried using "focus" events and the like, to no avail. How do I just select the text inside?

Any help is appreciated! Thanks!

"Try using onfocus" (on the cells in the table)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM