在 contenteditable 元素中自定义文本光标

发布于 2024-12-19 18:34:24 字数 117 浏览 1 评论 0原文

是否可以在 contenteditable="true" div 标记中自定义闪烁文本光标?

比如获取光标位置并在其上放置自定义光标,或者任何其他技巧?

Is it possible to customize blinking text cursor in a contenteditable="true" div tag?

Something like getting cursor position and putting a custom cursor on it, or any other trick?

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

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

发布评论

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

评论(2

破晓 2024-12-26 18:34:24

必须绘制自己的光标(例如,Google Docs 就是这样做的)。这样做是一项艰巨的任务,我不会推荐这样做。

Not without drawing your own cursor (which is what Google Docs does, for example). Doing this is a major undertaking to get right and I wouldn't recommend it.

ぽ尐不点ル 2024-12-26 18:34:24

我同意 Tim 的观点,我也不会推荐它。

(除非您有 Google 的 javascript 忍者之一来帮助您:p)

但是,这里是一个示例页面,其中包含在

我相信这是自定义插入符最基本的实现。

<!doctype html>
<html>
    <head>
        <script type="text/javascript">

            var cursor,
                $ = function (id){
                    return document.getElementById(id);
                },
                nl2br = function(txt){
                    return txt.replace(/\n/g, "<br />");
                },
                writeit = function(from, e){
                    e = e || window.event;
                    var w = $("writer");
                    var tw = from.value;
                    w.innerHTML = nl2br(tw);
                },
                moveIt = function (count, e){
                    e = e || window.event;
                    var keycode = e.keyCode || e.which;
                    if(keycode == 37 && parseInt(cursor.style.left) >= (0-((count-1)*10))){
                        cursor.style.left = parseInt(cursor.style.left) - 10 + "px";
                    } else if(keycode == 39 && (parseInt(cursor.style.left) + 10) <= 0){
                        cursor.style.left = parseInt(cursor.style.left) + 10 + "px";
                    }

                };

            window.onload = function (){
                cursor = $("cursor");               
                cursor.style.left = "0px";
            };

        </script>

        <style type="text/css">
            body{margin: 0px;padding: 0px;height: 99%;}
            textarea#setter{left: -1000px;position: absolute;}
            .cursor{font-size: 12px;background-color: red;color: red;position: relative;opacity: 0.5;}
            #terminal{margin: 8px;cursor: text;height: 500px;overflow: auto;}
            #writer{font-family: cursor, courier;font-weight: bold;}
            #getter{margin: 5px;}
        </style>
    </head>
    <body>
    <div id="terminal" onclick="$('setter').focus();">
        <textarea type="text" id="setter" onkeydown="writeit(this, event);moveIt(this.value.length, event)" onkeyup="writeit(this, event)" onkeypress="writeit(this, event);"></textarea>
        <div id="getter">
            <span id="writer"></span><b class="cursor" id="cursor">B</b>
        </div>
    </div>
    </body>
</html>

祝您在 contenteditable 中实现这一点好运。我当然希望这不会是您的下一个问题 (:p)!

I agree with Tim, I wouldn't recommend it either.

( unless you have one of Google's javascript ninjas to help you :p )

However, here is a sample page with a custom caret used in a <textarea>.. Just to give you an idea of how you can achieve this.

I beleive this is the most basic implementation of a custom caret.

<!doctype html>
<html>
    <head>
        <script type="text/javascript">

            var cursor,
                $ = function (id){
                    return document.getElementById(id);
                },
                nl2br = function(txt){
                    return txt.replace(/\n/g, "<br />");
                },
                writeit = function(from, e){
                    e = e || window.event;
                    var w = $("writer");
                    var tw = from.value;
                    w.innerHTML = nl2br(tw);
                },
                moveIt = function (count, e){
                    e = e || window.event;
                    var keycode = e.keyCode || e.which;
                    if(keycode == 37 && parseInt(cursor.style.left) >= (0-((count-1)*10))){
                        cursor.style.left = parseInt(cursor.style.left) - 10 + "px";
                    } else if(keycode == 39 && (parseInt(cursor.style.left) + 10) <= 0){
                        cursor.style.left = parseInt(cursor.style.left) + 10 + "px";
                    }

                };

            window.onload = function (){
                cursor = $("cursor");               
                cursor.style.left = "0px";
            };

        </script>

        <style type="text/css">
            body{margin: 0px;padding: 0px;height: 99%;}
            textarea#setter{left: -1000px;position: absolute;}
            .cursor{font-size: 12px;background-color: red;color: red;position: relative;opacity: 0.5;}
            #terminal{margin: 8px;cursor: text;height: 500px;overflow: auto;}
            #writer{font-family: cursor, courier;font-weight: bold;}
            #getter{margin: 5px;}
        </style>
    </head>
    <body>
    <div id="terminal" onclick="$('setter').focus();">
        <textarea type="text" id="setter" onkeydown="writeit(this, event);moveIt(this.value.length, event)" onkeyup="writeit(this, event)" onkeypress="writeit(this, event);"></textarea>
        <div id="getter">
            <span id="writer"></span><b class="cursor" id="cursor">B</b>
        </div>
    </div>
    </body>
</html>

Good luck implementing this in a contenteditable.. And I certainly hope this won't be your next question (:p) !

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