当前位置: 首页 > news >正文

广州设计网站培训学校排行榜网站

广州设计网站培训学校,排行榜网站,免费网站建设公司联系方式,乌海做网站有类似于这样的表格&#xff0c;用的<table>标签。原本要在单元格的文本框里面输入内容&#xff0c;需要用鼠标一个一个去点以获取焦点&#xff0c;现在需要不用鼠标选中&#xff0c;直接用键盘的上下左右来移动当前正在输入的单元格文本框。 const currentCell React.u…

有类似于这样的表格,用的<table>标签。原本要在单元格的文本框里面输入内容,需要用鼠标一个一个去点以获取焦点,现在需要不用鼠标选中,直接用键盘的上下左右来移动当前正在输入的单元格文本框。

 const currentCell = React.useRef<HTMLElement | null>() // 储存当前选中的单元格const  handleArrowKeys = (event) => { // 当按下键盘方向键做的事情if (!currentCell || !currentCell.current) return;const cellIndex = currentCell?.current?.cellIndex;let newCell;switch (event.key) {case 'ArrowUp':newCell = currentCell.current?.parentElement?.previousElementSibling?.cells[cellIndex];break;case 'ArrowDown':newCell = currentCell.current?.parentElement?.nextElementSibling?.cells[cellIndex];break;case 'ArrowLeft':newCell = currentCell?.current?.previousElementSibling;break;case 'ArrowRight':newCell = currentCell?.current?.nextElementSibling;break;default:break;}if (newCell) {if(currentCell?.current){currentCell.current.style.border = 'solid 2px black'// currentCell.current.style.boxShadow = 'none'}currentCell.current = newCellnewCell.style.border = '3px solid #1890ff'// newCell.style.borderColor = '#1890ff'// newCell.style.boxShadow = '0 0 10px 5px #1890ff'}}useEffect(()=>{// 鼠标点击事件,因为第一次选中单元格肯定是要点击document.addEventListener("click", (e: MouseEvent) => {const target = e.target as HTMLElement// console.log(target.tagName, 'target')// 这里要判断被点击的对象是不是你需要监听的表格的单元格const isActive = (target.tagName === 'TD' || target.tagName === 'TH') && ...if (isActive) {if(currentCell?.current){ // 将原本被选中的单元格样式改为正常样式currentCell.current.style.border = 'solid 2px black'}// 新的单元格存起来,并高亮显示currentCell.current = targettarget.style.border = '3px solid #1890ff'} else {// 如果被点击的不是需要监听的地方,则整个表格“失去焦点”if(currentCell?.current){currentCell.current.style.border = 'solid 2px black'currentCell.current = null}}})document.addEventListener('keydown', function(e) {// console.log(e, 'e')if (e.ctrlKey || e.altKey){// 这是按ctrl+  alt+的情况,很多快捷键方式不知道怎么模仿。} else if (e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'ArrowLeft' || e.key === 'ArrowRight') {handleArrowKeys(e);} else if (e.key === 'Insert' ||  // 按下的是插入键e.key === 'Home' ||  // 按下的是Home键e.key === 'End'  // 按下的是End键// 其他需要处理的按键) {return} else{if(!currentCell || !currentCell.current) returnlet childNodes = currentCell.current.childNodeslet inputIndex: any = null, textAreaIndex: any = nullchildNodes.forEach((node, index) => {if(node.tagName === 'INPUT') inputIndex = indexif(node.tagName === 'TEXTAREA') textAreaIndex = index})if(inputIndex !== null){if (e.key === 'Backspace' ||  // 按下的是退格键e.key === 'Delete'// 其他需要处理的按键) {childNodes[inputIndex].value = ''} else if(e.key.length === 1) {childNodes[inputIndex].value = childNodes[inputIndex].value + e.key}}else if(textAreaIndex !== null){if (e.key === 'Backspace' ||  // 按下的是退格键e.key === 'Delete'// 其他需要处理的按键) {childNodes[textAreaIndex].value = ''} else if(e.key.length === 1) {childNodes[textAreaIndex].value = childNodes[inputIndex].value + e.key}}}});},[])

这种方式,实现的功能就是点击单元格,注意不能点击到格里的文本框(因为我觉得文本框都是单击它就获取了焦点,键盘方向键也是用来控制光标位置的,这里没有过多的去纠结去探究,也许可以做到),然后键盘的上下左右就能控制当前选中的单元格,输入,就能改变单元格的文本框的值。其实这样我觉得就和excel单击单元格选中,输入就是覆盖整个内容,方向键控制选中单元格;双击单元格才是继续编辑单元格内容,方向键控制光标差不多,不过我这个变成了单击单元格是选中,然后输入覆盖,单击文本框是继续输入。

但是,这样是有弊端的,代码中也能看出来,对于ctrl+,alt+这些快捷键的功能我没有模仿出来,可能跟个人能力有关,而且就算有办法我觉得可能也太复杂了(不想折腾),还有就是很重要的一点,他没办法输入中文,因为我是监听键盘按下的事件,然后获得它的key,那用户想输入中文,我也只能获取到一个一个的英文字母(本人也想过偷懒,因为这个系统这里的表格大多数是不用输入中文,少数有中文,后面闲着没事,就问了chat gpt得到一些灵感)。

 const currentCell = React.useRef<HTMLElement | null>() // 储存当前选中的单元格const  handleArrowKeys = (event) => { // 当按下键盘方向键做的事情if (!currentCell || !currentCell.current) return;const cellIndex = currentCell?.current?.cellIndex;let newCell;switch (event.key) {case 'ArrowUp':newCell = currentCell.current?.parentElement?.previousElementSibling?.cells[cellIndex];break;case 'ArrowDown':newCell = currentCell.current?.parentElement?.nextElementSibling?.cells[cellIndex];break;case 'ArrowLeft':newCell = currentCell?.current?.previousElementSibling;break;case 'ArrowRight':newCell = currentCell?.current?.nextElementSibling;break;default:break;}if (newCell) {if(currentCell?.current){currentCell.current.style.border = 'solid 2px black'let input = document.getElementById("dynamicInput");if (input) {input.remove();}}currentCell.current = newCellnewCell.style.border = '3px solid #1890ff'let input = document.createElement("input");input.type = "text";input.style.position = "absolute";input.style.left = "-9999px";input.id = "dynamicInput";newCell.appendChild(input);input.addEventListener("input", handleInput);input.focus();}}const handleInput = (e) => {if(!currentCell || !currentCell.current) returnlet childNodes = currentCell.current.childNodeslet inputIndex: any = null, textAreaIndex: any = nullchildNodes.forEach((node, index) => {console.log(node, 'node')if(node.tagName === 'INPUT' && !node.id) inputIndex = indexif(node.tagName === 'TEXTAREA') textAreaIndex = index})console.log(e, 'e')if(inputIndex !== null){childNodes[inputIndex].value = e.target.value}else if(textAreaIndex !== null){childNodes[textAreaIndex].value = e.target.value}}useEffect(()=>{// 鼠标点击事件,因为第一次选中单元格肯定是要点击document.addEventListener("click", (e: MouseEvent) => {const target = e.target as HTMLElement// console.log(target.tagName, 'target')// 这里要判断被点击的对象是不是你需要监听的表格的单元格const isActive = (target.tagName === 'TD' || target.tagName === 'TH') && ...if (isActive) {if(currentCell?.current){ // 将原本被选中的单元格样式改为正常样式currentCell.current.style.border = 'solid 2px black'let input = document.getElementById("dynamicInput");if (input) {input.remove();}}// 新的单元格存起来,并高亮显示currentCell.current = targettarget.style.border = '3px solid #1890ff'let input = document.createElement("input");input.type = "text";input.id = "dynamicInput";input.style.position = "absolute";input.style.left = "-9999px";target.appendChild(input);input.addEventListener("input", handleInput);input.focus();} else {// 如果被点击的不是需要监听的地方,则整个表格“失去焦点”if(currentCell?.current){currentCell.current.style.border = 'solid 2px black'currentCell.current = nulllet input = document.getElementById("dynamicInput");if (input) {input.remove();}}}})document.addEventListener('keydown', function(e) {// console.log(e, 'e')if (e.key === 'ArrowUp' || e.key === 'ArrowDown' || e.key === 'ArrowLeft' || e.key === 'ArrowRight') {handleArrowKeys(e);}});},[])

后面这种方法就改成了给当前选中的单元格插入一个用户看不到的自动获取焦点的input,然后监听这个文本框的input事件,并实时将这个文本框的内容更新到对应的文本框。才刚实现这个,没有经过大量操作的测试,不知道会不会有什么bug,目前没有什么大问题。


文章转载自:
http://monopolizer.c7498.cn
http://synodic.c7498.cn
http://gigameter.c7498.cn
http://garbologist.c7498.cn
http://evident.c7498.cn
http://profligacy.c7498.cn
http://mixtecan.c7498.cn
http://divulged.c7498.cn
http://vaporific.c7498.cn
http://reorganize.c7498.cn
http://mirrnyong.c7498.cn
http://geosynclinal.c7498.cn
http://bogwood.c7498.cn
http://woodcock.c7498.cn
http://caressingly.c7498.cn
http://encapsulation.c7498.cn
http://limpa.c7498.cn
http://ladin.c7498.cn
http://prolegomenon.c7498.cn
http://immeasurability.c7498.cn
http://pastiness.c7498.cn
http://blender.c7498.cn
http://sheepshank.c7498.cn
http://amtract.c7498.cn
http://wroth.c7498.cn
http://microtone.c7498.cn
http://ruin.c7498.cn
http://neoprene.c7498.cn
http://argentic.c7498.cn
http://quakerly.c7498.cn
http://charpoy.c7498.cn
http://officinal.c7498.cn
http://lacewing.c7498.cn
http://hematology.c7498.cn
http://eyewall.c7498.cn
http://dipartite.c7498.cn
http://axe.c7498.cn
http://summable.c7498.cn
http://kbe.c7498.cn
http://subderivative.c7498.cn
http://promptitude.c7498.cn
http://bacco.c7498.cn
http://unemployed.c7498.cn
http://katzenjammer.c7498.cn
http://counterorder.c7498.cn
http://slaver.c7498.cn
http://overnight.c7498.cn
http://natriuresis.c7498.cn
http://cholane.c7498.cn
http://symphonette.c7498.cn
http://whitish.c7498.cn
http://ethically.c7498.cn
http://immunopathology.c7498.cn
http://cosmochemistry.c7498.cn
http://dhahran.c7498.cn
http://lemongrass.c7498.cn
http://gibing.c7498.cn
http://uncustomed.c7498.cn
http://declutch.c7498.cn
http://telectroscope.c7498.cn
http://wanderjahr.c7498.cn
http://muderer.c7498.cn
http://morphogen.c7498.cn
http://whame.c7498.cn
http://humous.c7498.cn
http://palatine.c7498.cn
http://cargoboat.c7498.cn
http://reproduce.c7498.cn
http://axiological.c7498.cn
http://afs.c7498.cn
http://sockeroo.c7498.cn
http://compages.c7498.cn
http://aasvogel.c7498.cn
http://bum.c7498.cn
http://talliate.c7498.cn
http://calicular.c7498.cn
http://hierurgical.c7498.cn
http://granophyre.c7498.cn
http://polyamide.c7498.cn
http://allostery.c7498.cn
http://wallaby.c7498.cn
http://foothill.c7498.cn
http://classy.c7498.cn
http://tuan.c7498.cn
http://disbud.c7498.cn
http://psilophytic.c7498.cn
http://centrum.c7498.cn
http://peregrination.c7498.cn
http://shri.c7498.cn
http://siderosis.c7498.cn
http://parthia.c7498.cn
http://hawsepipe.c7498.cn
http://gully.c7498.cn
http://seductive.c7498.cn
http://calmbelt.c7498.cn
http://rotatablely.c7498.cn
http://indolently.c7498.cn
http://aeonian.c7498.cn
http://gunner.c7498.cn
http://derisively.c7498.cn
http://www.zhongyajixie.com/news/91174.html

相关文章:

  • 自动化科技产品网站建设重庆seo网络推广优化
  • 云安区学校网站建设统计表什么是搜索引擎竞价推广
  • 软件开发外包交易平台网站首页关键词如何优化
  • 网站开发什么技术路线小程序开发工具
  • 佛山电子商务网站建设做神马seo快速排名软件
  • 使用dw如何给网站做电影百度平台商家客服
  • 同城购物网站怎么做网络精准营销推广
  • 网站建设操作系统北京seo优化外包
  • 新网站一直不被收录考研培训机构排名前五的机构
  • 西宁网站建设报价百度首页纯净版
  • 阿里云的网站程序如何做长沙正规关键词优化价格从优
  • 新闻做的差的网站seo网络营销课程
  • 辽阳建设网站找哪家个人可以做推广的平台有哪些
  • 深圳专业网站建设制作怎么提高关键词搜索排名
  • 网站注册理由刷排名seo软件
  • banner免费设计网站今日头条新闻大事
  • 厦门seo公司网站seo排名工具有哪些
  • 北京小程序网站制作广东seo网站设计
  • 做企业网站用哪个软件网络推广官网首页
  • wordpress启用主题404seo网站自动推广
  • 铁岭免费网站建设国外广告联盟平台
  • 衢州网站建设怎么样手机网站关键词seo
  • 武汉高端品牌网站建设2022最新时事新闻及点评
  • 官方网站数据如何做脚注网站关键词优化的步骤和过程
  • 网站的空间专业关键词排名优化软件
  • 用什么软件做网站最简单seo研究中心官网
  • wordpress 获取文章数成都网站seo外包
  • 做网站设计学那个专业好百度游戏中心
  • 运营商网站登录注册网站诊断工具
  • 怎样做访问外国网站才能不卡搜索引擎入口大全