Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t清除文本区域中的文本选择 问题:_Javascript_Reactjs - Fatal编程技术网

Javascript Can';t清除文本区域中的文本选择 问题:

Javascript Can';t清除文本区域中的文本选择 问题:,javascript,reactjs,Javascript,Reactjs,我有多个textareas,可以使用箭头键进行导航。使用ref.focus() 当textareas以这种方式聚焦时,文本选择是否未清除 截图 期待 单击第一个textarea或再次聚焦第二个textarea时,应清除第二个textarea中的文本选择 代码 import React,{useffect,useRef,useState}来自“React”; 导出常量测试=()=>{ 常量[editingBlock,setEditingBlock]=useState(null); const t

我有多个
textarea
s,可以使用箭头键进行导航。使用
ref.focus()

textarea
s以这种方式聚焦时,文本选择是否未清除

截图

期待 单击第一个
textarea
或再次聚焦第二个
textarea
时,应清除第二个
textarea
中的文本选择

代码
import React,{useffect,useRef,useState}来自“React”;
导出常量测试=()=>{
常量[editingBlock,setEditingBlock]=useState(null);
const textArea1Ref=useRef(null);
const textArea2Ref=useRef(null);
useffect(()=>{
//设定1焦点
if(编辑块===1&&textArea1Ref.current){
textArea1Ref.current.focus();
//模糊2
如果(文本区域2ref.current){
textArea2Ref.current.blur();
}
//设定2焦点
}else if(editingBlock==2&&textArea2Ref.current){
textArea2Ref.current.focus();
//模糊1
如果(文本区域1参考当前){
textArea1Ref.current.blur();
}
}
},[editingBlock]);
返回(
setEditingBlock(空)}
onKeyDown={(e)=>{
如果(e.key==“ArrowDown”)设置编辑块(2);
}}
onClick={(e)=>setEditingBlock(1)}
/>
{
if(window.getSelection()){
window.getSelection()!.removeAllRanges();//不起作用
}
setEditingBlock(空);
}}
onClick={(e)=>setEditingBlock(2)}
/>
);
};
该值将始终相同 您已经使用字符串设置了该值,这将永远不会更改,因为它是字段中的静态值

如果要使字段可变,请使用一些状态、减速机或库来处理字段

这里有一个例子

const [textArea1, setTextArea1] = useState<string>('');
...
return (
  ...
  <textarea
     ref={textArea1Ref}
     value={textArea1}
     onChange={(event) => setTextArea1(event.target.value)}
  />
  ...
)
const[textArea1,setTextArea1]=useState(“”);
...
返回(
...
setTextArea1(event.target.value)}
/>
...
)
要清除模糊上的字段,只需使用fire
setTextArea1(“”)

值将始终相同 您已经使用字符串设置了该值,这将永远不会更改,因为它是字段中的静态值

如果要使字段可变,请使用一些状态、减速机或库来处理字段

这里有一个例子

const [textArea1, setTextArea1] = useState<string>('');
...
return (
  ...
  <textarea
     ref={textArea1Ref}
     value={textArea1}
     onChange={(event) => setTextArea1(event.target.value)}
  />
  ...
)
const[textArea1,setTextArea1]=useState(“”);
...
返回(
...
setTextArea1(event.target.value)}
/>
...
)
要清除blur上的字段,只需使用fire
setTextArea1(“”)

Hacky解决方案即可 我仍然不完全理解为什么会发生这种情况,但现在找到了一个变通解决方案:

textarea
onBlur
回调中,只需使用以下代码:

if(textArea2Ref.current){
//如果有一个范围选择
if(textArea2Ref.current.selectionStart!==textArea2Ref.current.selectionEnd){
//重置范围
textArea2Ref.current.selectionStart=textArea2Ref.current.selectionEnd;
//再次模糊文本区域,因为设置选择会聚焦文本区域?
textArea2Ref.current.blur();
}
}
Hacky解决方案 我仍然不完全理解为什么会发生这种情况,但现在找到了一个变通解决方案:

textarea
onBlur
回调中,只需使用以下代码:

if(textArea2Ref.current){
//如果有一个范围选择
if(textArea2Ref.current.selectionStart!==textArea2Ref.current.selectionEnd){
//重置范围
textArea2Ref.current.selectionStart=textArea2Ref.current.selectionEnd;
//再次模糊文本区域,因为设置选择会聚焦文本区域?
textArea2Ref.current.blur();
}
}

我想你误解了我的问题,也许我不够清楚。但我试图清除选择(突出显示),而不是文本本身。字符串是静态的,因为这是不相关的。啊,对不起,那么是的,我确实误解了。让我再看看。我想你误解了我的问题,也许我不够清楚。但我试图清除选择(突出显示),而不是文本本身。字符串是静态的,因为这是不相关的。啊,对不起,那么是的,我确实误解了。让我再看看。