HTML / JAVASCRIPT :在 contentEditable=true 中禁用 HTML 内容

发布于 2024-09-11 12:16:28 字数 557 浏览 9 评论 0原文

我想要什么?


我想要一个像textarea一样工作的div,我不想在div中编辑内容并粘贴图像等只是纯文本。

示例


www.facebook.com - 最好的示例facebook 的新闻 feed。

为什么我需要这种方式?


如果您查看 Facebook 的新闻源,您会发现您可以撰写帖子的区域会在您撰写帖子或点击某个按钮时展开很多进入。

这与我想使用带有 contentEditable 的 div 的原因相同,因为在 textarea 中我不能这样做。 #

请不要使用 JQUERY,仅使用 JAVASCRIPT

What I want?


I want a div what works like a textarea, I don't want to have the ability to edit things in the div, and paste images and so on just plain text.

Example


www.facebook.com
- The best example is facebook's news feed.

Why I need this way?


If you check out facebook's news feed, you well see that the area where you can write your post, expands as you write your post or hit a lots of enters.

This is the same reason why I want to use a div with contentEditable, because in textarea I can't do that.
#

PLEASE NO JQUERY only JAVASCRIPT

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

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

发布评论

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

评论(3

南笙 2024-09-18 12:16:28

使用没有框架的纯 JavaScript 来调整大小的 Textarea:

<html>
    <head>
        <script>
            function taOnInput()
            {
                var dis = this;
                setTimeout(
                    function(){
                        var span = document.createElement("div");
                        span.innerHTML = escape(dis.value).replace(/[%]0A/g, "<br/>")+"<br/>."; //Extra BR for padding... TextArea uses %0A, not \n
                        span.style.width = dis.offsetWidth+"px";
                        span.style.padding = "0px";
                        span.style.fontFamily = "Lucida Console";
                        document.body.appendChild(span); //Offset height doesnt work when not in DOM tree i guess =/? or is it a hack
                        dis.style.height = span.offsetHeight+"px";
                        document.body.removeChild(span);
                    }, 1
                ); //setTimeout=hack, since oKP is called BEFORE character append.  
            }
            window.onload = function()
            {
                var resizableTA = document.getElementById("resizableTA");
                resizableTA.onkeypress = taOnInput;
            }
        </script>
        <title>ItzWarty - Untitled Document</title>
    </head>
    <body>
        <textarea id="resizableTA">Trololololol</textarea>
    </body>
</html>

非常 hackish,不到 10 分钟就将其组合在一起。希望它至少能给你一个想法。

仅在 Google Chrome 5.0.308.0 上进行了测试

代码说明,因为我无法评论
1) 在window.onload之前,id为“ressizedTA”的textarea已经被创建并附加到DOM树的document.body中。
2) window.onload 附加一个事件处理程序,taOnInput [输入上的文本区域]。
3)输入上的textarea创建一个虚拟span,强制其宽度为textarea的宽度,字体样式为“Lucida Console”,据我所知是textarea的默认字体,将textarea的值复制到span的innerHTML,同时替换%0A [文本区域使用的换行符] 与
[换行符]...
4)span的offsetHeight是span的高度,现在可以用它来强制textarea的高度。

谁能确认 Lucida Console 是 textarea 的默认字体吗?是康索拉吗?快递新?我假设任何固定宽度的字体都可以工作。我不使用Mac,所以我不知道它与Windows共享什么字体,尽管我认为Courier New是更好的选择......

Resizable Textarea using pure JavaScript without frameworks:

<html>
    <head>
        <script>
            function taOnInput()
            {
                var dis = this;
                setTimeout(
                    function(){
                        var span = document.createElement("div");
                        span.innerHTML = escape(dis.value).replace(/[%]0A/g, "<br/>")+"<br/>."; //Extra BR for padding... TextArea uses %0A, not \n
                        span.style.width = dis.offsetWidth+"px";
                        span.style.padding = "0px";
                        span.style.fontFamily = "Lucida Console";
                        document.body.appendChild(span); //Offset height doesnt work when not in DOM tree i guess =/? or is it a hack
                        dis.style.height = span.offsetHeight+"px";
                        document.body.removeChild(span);
                    }, 1
                ); //setTimeout=hack, since oKP is called BEFORE character append.  
            }
            window.onload = function()
            {
                var resizableTA = document.getElementById("resizableTA");
                resizableTA.onkeypress = taOnInput;
            }
        </script>
        <title>ItzWarty - Untitled Document</title>
    </head>
    <body>
        <textarea id="resizableTA">Trololololol</textarea>
    </body>
</html>

Very hackish, put it together in less than 10 minutes. Hopefully it'll at least give you an idea.

ONLY tested on Google Chrome 5.0.308.0

Explanation of code, since i fail at commenting
1) before window.onload, the textarea of id "resizableTA" has been created and appended to document.body of DOM tree.
2) window.onload attaches an event handler, taOnInput [textarea on input].
3) textarea on input creates a dummy span, forces its width to the width of the textarea and font style to "Lucida Console", which AFAIK is the default font for textareas, copies the value of the textarea to the span's innerHTML, while replacing %0A [newline that textareas use] with
[line break]...
4) span's offsetHeight is the height of the span, which can now be used to force the height of the textarea.

Can anyone confirm that Lucida Console is the default font of textarea? Is it Consola? Courier New? I assumed any fixed-width font would work. I don't use Mac, so I dont know what fonts it shared with windows, though i think Courier New is a better choice...

不羁少年 2024-09-18 12:16:28

您不必使用 DIV。您仍然可以拥有一个文本区域并在必要时将其展开。

这是一个 jQuery 插件,它就是这样做的: http://plugins.jquery.com/project/TextAreaResizer< /a>

这是一个演示: http://www.itsavesyou.com/TextArea_Resizer_example.htm

You don't have to use a DIV. You can still have a textarea and expand it when necessary.

Here is a jQuery plugin that does just that: http://plugins.jquery.com/project/TextAreaResizer

Here is a demo: http://www.itsavesyou.com/TextArea_Resizer_example.htm

坏尐絯℡ 2024-09-18 12:16:28

在我看来,最好的解决方案仍然是使用 textarea 元素,并使用 Javascript 根据需要调整其大小以响应内容更改。

默认情况下,Textarea 元素确实允许手动调整大小(这可以通过“resize”CSS 属性进行控制。但是,实现自动扩展多行文本字段而不丰富格式的网站主要使用 textarea 来实现结合样式和 Javascript。

It seems to me that the best solution to this is still to use a textarea element, and to use Javascript to resize it as needed in response to content changes.

Textarea elements do allow for manual resizing by default (and this can be controlled by the "resize" CSS property. But sites that implement auto-expanding multi line text fields without rich formatting would primarily do so with textareas combined with styling and Javascript.

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