如何使用 JavaScript 控制 Textarea 中的行为

发布于 2025-01-16 17:02:53 字数 2638 浏览 4 评论 0原文


i am currently trying to design a Terminal in JavaScript. Unfortunately do i have an issue in tracking where something is getting included or deleted. Here a possible Solution (https://stackoverflow.com/questions/7745867/how-do-you-get-the-cursor-position-in-a-textarea), but i do not really like it and it would be great, if there were an other option that is easier to include.
Here is what i have:

wp.​​html

<!DOCTYPE html>
<html lang="de">
    <title>CODO</title>
    <head>
        <title>Codo</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" media="screen" href="main.css">
    </head>
    <body>
        <button id="ist_da">Ist da</button> 
        <textarea id = "Codo" class = "codo_text_area"></textarea>
        <script src="codo.js"></script>
        <script src="ist_da.js"></script>
    </body>
</html>

main.css

.codo_text_area{
    background-color: rgb(25, 25, 25);
    color: rgb(5, 255, 5);
    height: 90%;
    width: 40%;
    position:absolute;
    left: 100px;
    right:auto;
    border: 20px solid gray;
    resize: none;
}

codo.js

let ist_da_Button = document.getElementById("ist_da");
ist_da_Button.addEventListener("click",ist_da);

let el_codo = document.getElementById("Codo");
let el_cols = check_cols()
el_codo.value = ">>>"

el_codo.addEventListener("focus",codo_onfocus);
el_codo.addEventListener("blur",codo_blur)
el_codo.addEventListener("keydown",codo_keydown)

//EventListener for behavior
function codo_keydown(e){
    if(e.code == "Enter"){
        el_codo.value = el_codo.value + "\n>>>"
        e.preventDefault();
    }else if(e.code == "Backspace" && el_codo.value.endsWith("\n>>>")){
        el_codo.value = el_codo.value.substring(0,el_codo.value.lastIndexOf("\n>>>"))
        e.preventDefault()
    }else if(e.code == "Backspace" && el_codo.value == ">>>"){
        e.preventDefault()
    }
}

function codo_blur(){
    el_codo.style.color = "rgb(5,200,5)"
}

function codo_onfocus(){
    el_codo.style.color = "rgb(5,255,5)"
}


//gives back how many line are in text
function check_cols(){
    let v = el_codo.value;
    let g = 0;
    for(let c in v){
        if(v[c] == "\n"){
            g = g + 1;
        }
    }
    return g
}

如果您有任何方法可以以任何可能的方式改进这一点,请告诉我。毕竟 JavaScript 对我来说是新的,非常感谢提示

i am currently trying to design a Terminal in JavaScript.
Unfortunately do i have an issue in tracking where something is getting included or deleted.
Here a possible Solution (https://stackoverflow.com/questions/7745867/how-do-you-get-the-cursor-position-in-a-textarea), but i do not really like it and it would be great, if there were an
other option that is easier to include.
Here is what i have:

wp.html

<!DOCTYPE html>
<html lang="de">
    <title>CODO</title>
    <head>
        <title>Codo</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" type="text/css" media="screen" href="main.css">
    </head>
    <body>
        <button id="ist_da">Ist da</button> 
        <textarea id = "Codo" class = "codo_text_area"></textarea>
        <script src="codo.js"></script>
        <script src="ist_da.js"></script>
    </body>
</html>

main.css

.codo_text_area{
    background-color: rgb(25, 25, 25);
    color: rgb(5, 255, 5);
    height: 90%;
    width: 40%;
    position:absolute;
    left: 100px;
    right:auto;
    border: 20px solid gray;
    resize: none;
}

codo.js

let ist_da_Button = document.getElementById("ist_da");
ist_da_Button.addEventListener("click",ist_da);

let el_codo = document.getElementById("Codo");
let el_cols = check_cols()
el_codo.value = ">>>"

el_codo.addEventListener("focus",codo_onfocus);
el_codo.addEventListener("blur",codo_blur)
el_codo.addEventListener("keydown",codo_keydown)

//EventListener for behavior
function codo_keydown(e){
    if(e.code == "Enter"){
        el_codo.value = el_codo.value + "\n>>>"
        e.preventDefault();
    }else if(e.code == "Backspace" && el_codo.value.endsWith("\n>>>")){
        el_codo.value = el_codo.value.substring(0,el_codo.value.lastIndexOf("\n>>>"))
        e.preventDefault()
    }else if(e.code == "Backspace" && el_codo.value == ">>>"){
        e.preventDefault()
    }
}

function codo_blur(){
    el_codo.style.color = "rgb(5,200,5)"
}

function codo_onfocus(){
    el_codo.style.color = "rgb(5,255,5)"
}


//gives back how many line are in text
function check_cols(){
    let v = el_codo.value;
    let g = 0;
    for(let c in v){
        if(v[c] == "\n"){
            g = g + 1;
        }
    }
    return g
}

If you have any ways to improve this in any way possible just tell me. JavaScript is after all new for me and tips are greatly appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤星 2025-01-23 17:02:53

这是对我有用的解决方案:

function cursorPosition() {
var content = el_codo;
if((el_codo.selectionStart!=null)&&(el_codo.selectionStart!=undefined)){
    return content.selectionStart;
}
else {
    return false;
}

}

This is the solution, that worked for me:

function cursorPosition() {
var content = el_codo;
if((el_codo.selectionStart!=null)&&(el_codo.selectionStart!=undefined)){
    return content.selectionStart;
}
else {
    return false;
}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文