简体   繁体   English

如何将光标/文本位置指示器添加到<textarea>由javascript控制?

[英]How to add a cursor/text position indicator to a <textarea> controlled by javascript?

I am making a typewriter like website ( here ) that uses a textarea and javascript (from button clicks) to add/remove from the textarea.我正在制作一个像网站(这里)这样的打字机,它使用 textarea 和 javascript(来自按钮点击)从 textarea 添加/删除。

The next thing I want to add is an indicator to show where the next update (insert/remove) will be so I guess its always at the of the textarea value.我想添加的下一件事是一个指示器,用于显示下一次更新(插入/删除)的位置,所以我猜它总是在 textarea 值的位置。

The key details that clash with other solutions are与其他解决方案冲突的关键细节是

  • The textarea is disabled (+ its a textarea and not ap tag) textarea 被禁用(+ 它是一个 textarea 而不是 ap 标签)
  • Users add characters by using the buttons and not by typing with their keyboards用户通过使用按钮而不是通过键盘输入来添加字符
  • The user isn't selecting or has a selection while typing用户在打字时没有选择或有选择
  • Some solutions involve css that break the website一些解决方案涉及破坏网站的css

This is how I insert characters这就是我插入字符的方式

function insert(char) {
    document.getElementById("textarea").value += char
    update()
}

to the textarea + a button到 textarea + 一个按钮

<textarea id="textarea" class="textarea" disabled>
</textarea>

<a onclick="insert('1')"> 
    <div class="letterbox">
        <p class="typewritter">1</p>
    </div> 
</a>

(btw I know I spelled typewriter wrong on the website and will fix that later) (顺便说一句,我知道我在网站上拼错了打字机,稍后会修复)

I think overriding textarea attributes is meaningless.我认为覆盖 textarea 属性是没有意义的。 The indicator that you want is already in it's characteristic.您想要的指标已经在它的特性中。

Remove the disabled tag from your textarea and just block the keyboard pressing with the below code snippet.从您的textarea删除disabled标签,并使用以下代码片段阻止键盘按下。 [because not using keyboard typing is your feature] [因为不使用键盘打字是你的特点]

document.onkeydown = function (e)  {
  return false;
} 

If you want to you can bind the function to focus action of the textarea .如果你愿意,你可以将函数绑定到textarea焦点操作。

My suggestion on this would be to have the textarea active, NOT disabled and always on focus.我对此的建议是让 textarea 处于活动状态,而不是禁用并始终处于焦点。 This way you will have the blinking effect you want.这样你就会得到你想要的闪烁效果。 Then have a pointer-events:none css on the textarea so no one can click on it and you have the blink you want.然后在 textarea 上有一个pointer-events:none css,这样没有人可以点击它,你就有了你想要的眨眼。 So my steps would be:所以我的步骤是:

  1. Remove disabled from textarea从 textarea 中删除disabled
  2. Add pointer-events:none css rule to the textareapointer-events:none css 规则添加到 textarea
  3. On the update() function I observe add either a focus or a click event so the textarea gets focus on each insert()update()函数上,我观察添加一个focus或一个click事件,以便 textarea 将焦点放在每个insert()

If you want to make a custom insertion point (the blinking line) you can do that by adding and removing a chosen symbol ('|' for example) from the text with an interval.如果您想创建自定义插入点(闪烁的线),您可以通过从文本中添加和删除所选符号(例如“|”)来实现。 Perhaps something like this would do:也许这样的事情会做:

const insertionSymbol = '|';
const blinkInterval = 1000; // time in ms between insertion point updates
let blinking = true; // whether the insertion line is hidden
let textarea = document.getElementById('textarea');
setInterval(function() {
    if(blinking) {
        textarea.value += insertionSymbol;
    } else {
        textarea.value = textarea.value.slice(0,-1);
    }
    blinking = !blinking;
}, blinkInterval);

that would require you to change the insert and delete functions:这将需要您更改插入和删除功能:

function insert(char) {
    if(blinking) {
        textarea.value += char;
    } else {
        // inserting the character before the insertion point by removing the insertion point temporarily 
        textarea.value = textarea.value.slice(0,-1) + char + insertionSymbol; 
    }
}
function delete() {
    if(blinking) {
        textarea.value = textarea.slice(0,-1);
    } else {
        // deleting the character before the insertion point by removing the insertion point temporarily 
        textarea.value = textarea.value.slice(0,-2) + insertionSymbol; 
    }
}

This idea can be expanded.这个想法可以扩展。 for example, if you want to add a insertion point cool-down (ie change the insertion point to stay visible for some time after an update, like you see in most text editors), you can change the interval to run every millisecond, and add a timer for the next update.例如,如果您想添加一个插入点冷却(即更改插入点以在更新后保持可见一段时间,就像您在大多数文本编辑器中看到的那样),您可以将间隔更改为每毫秒运行一次,并且为下一次更新添加一个计时器。 like this:像这样:

// ... all the variable definitions like before
let timer = blinkInterval;
setInterval(function() {
    timer --;
    if(timer == 0) {
        if(blinking) {
            textarea.value += insertionSymbol;
        } else {
            textarea.value = textarea.value.slice(0,-1);
        }
        blinking = !blinking;
        timer = blinkInterval;
    }
}, 1);
function insert(char) {
    if(blinking) {
        blinking = false;
        textarea.value += char + insertionSymbol;
    } else {
        // inserting the character before the insertion point by removing the insertion point temporarily 
        textarea.value = textarea.value.slice(0,-1) + char + insertionSymbol; 
    }
    timer = blinkInterval;
}
function delete() {
    if(blinking) {
        blinking = false;
        textarea.value = textarea.slice(0,-1) + insertionSymbol;
    } else {
        // deleting the character before the insertion point by removing the insertion point temporarily 
        textarea.value = textarea.value.slice(0,-2) + insertionSymbol; 
    }
    timer = blinkInterval;
}

Note: I'm writing the code blindly (I didn't run it to see if it works) so I'm sorry in advance for any mistakes.注意:我是盲目地编写代码(我没有运行它以查看它是否有效),因此对于任何错误,我提前表示歉意。

One way to do it would be to have a loop running non stop (with a requestAnimationFrame maybe ?) to add and remove a |一种方法是让循环不停地运行(可能带有requestAnimationFrame ?)来添加和删除| character every other second, which would simulate the usual text position indicator you have inside most text areas每隔一秒的字符,这将模拟您在大多数文本区域内的常用文本位置指示器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将文本添加到TextArea中光标当前在何处通过快捷键触发? - Add Text to TextArea Where Cursor is currently in position Trigger By Shortcut Key? 将文本输入定位在textarea光标位置的顶部(javascript,Textarea autcompleting) - Positioning a text input on top of a textarea's cursor position (javascript, Textarea autcompleting) 自动完成Textarea-使用JavaScript将文本输入放置在textarea光标位置的顶部 - Textarea autcompleting - Positioning a text input on top of a textarea's cursor position using JavaScript 如何使用JavaScript在文本区域上显示光标? - How to appear cursor on textarea with JavaScript? 如何滚动到文本区域中光标的位置? - How do you scroll to the position of the cursor in a textarea? 你如何获得文本区域中的光标位置? - How do you get the cursor position in a textarea? 如何获得由javascript html编辑器控制的textarea的值? - How to get value of a textarea which is controlled by a javascript html editor? 如何使用 javascript 找出文本编辑器的 cursor position - How to find out the cursor position for text editor using javascript 如何使用JavaScript添加和合并2个textarea元素的文本值,并在第三个textarea中填充合并的文本? - How to add and combine text value of 2 textarea elements and populate the combined text in a third textarea using JavaScript? 如何通过在javascript中按Enter键向textarea添加文本 - How to add text to textarea by pressing the enter key in javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM