gpt4 book ai didi

javascript - onselectionchange 更新时变量不更新

转载 作者:行者123 更新时间:2023-12-03 01:01:19 30 4
gpt4 key购买 nike

我有以下代码,当我在文本框中选择文本时,应更新两个变量:selectionselectionArray。然而,这种情况并非如此。当我运行代码时,selectionArray 会发出警报,它显示变量的原始值,即 null。为什么变量没有更新?

<textarea name="" id="textbox" cols="30" rows="10"></textarea>

var textbox = document.getElementById('textbox');
var selection = null;
var selectionArray = null;

document.onselectionchange = () => {
if(document.getSelection().toString() != '') {
selection = document.getSelection().toString();
selecionArray = [...selection];
alert(selectionArray);

}
};

最佳答案

就目前情况而言,您有一个拼写错误:selecionArray = [...selection];(相对于selectionArray)。如果这是在您的实际代码中,并且不仅仅是在编写问题的转录过程中出现错误...哎呀,这就是您的答案。

关于javascript - onselectionchange 更新时变量不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52614573/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com