Javascript:在 html textarea 和 nicedit 之间切换时出现问题

发布于 2024-10-14 22:09:42 字数 1851 浏览 3 评论 0原文

我正在使用 nicedit

我一直在尝试创建一个默认的 html 文本区域,当您单击它时,它会变成 nicedit 文本字段。我希望它在失去焦点时恢复为纯 html 文本区域。当我只使用一个文本区域时,我已经能够成功地做到这一点,但是,当我使用两个文本区域时,会发生奇怪的事情(在 Firefox 中)。我使用以下脚本:

<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript"> 

function fieldname_1()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname');
      area = null;
      document.getElementById("fieldname").onclick=function(){fieldname_2()}
   });
}

function fieldname_2()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname');
      area = null;
      document.getElementById("fieldname").onclick=function(){fieldname_1()}
   });
}

function fieldname2_1()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname2');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname2');
      area = null;
      document.getElementById("fieldname2").onclick=function(){fieldname2_2()}
   });
}

function fieldname2_2()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname2');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname2');
      area = null;
      document.getElementById("fieldname2").onclick=function(){fieldname2_1()}
   });
}

</script>

<TEXTAREA id="fieldname" cols="35" onclick="fieldname_1();" ></TEXTAREA>
<br><br><br>
<TEXTAREA id="fieldname2" cols="35" onclick="fieldname2_1();" >Test text</TEXTAREA>

您单击并取消焦点的第一个文本区域工作得很好,但是,您单击的第二个文本区域在尝试取消焦点时不会消失。我做错了什么?

I am using nicedit.

I have been trying to create a default html textarea that gets turned into a nicedit textfield when you click on it. I want it to revert to a plain html textarea when it loses focus. I have been able to do this successfully when using just one textarea, however, when I use two textareas strange things happen (in Firefox). I use the following script:

<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> <script type="text/javascript"> 

function fieldname_1()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname');
      area = null;
      document.getElementById("fieldname").onclick=function(){fieldname_2()}
   });
}

function fieldname_2()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname');
      area = null;
      document.getElementById("fieldname").onclick=function(){fieldname_1()}
   });
}

function fieldname2_1()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname2');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname2');
      area = null;
      document.getElementById("fieldname2").onclick=function(){fieldname2_2()}
   });
}

function fieldname2_2()
{
   area = new nicEditor({fullPanel : true}).panelInstance('fieldname2');
   area.addEvent('blur', function() 
   {
      area.removeInstance('fieldname2');
      area = null;
      document.getElementById("fieldname2").onclick=function(){fieldname2_1()}
   });
}

</script>

<TEXTAREA id="fieldname" cols="35" onclick="fieldname_1();" ></TEXTAREA>
<br><br><br>
<TEXTAREA id="fieldname2" cols="35" onclick="fieldname2_1();" >Test text</TEXTAREA>

The first textarea you click and unfocus from works perfectly, however, the second textarea you click will not disappear when trying to unfocus. What am I doing wrong?

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

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

发布评论

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

评论(2

憧憬巴黎街头的黎明 2024-10-21 22:09:42

你不能用超过一个的文本区域来完成这个技巧,那么 1000 个呢?

<!DOCTYPE html>

<html>
    <head>
        <meta charset = "utf-8">

        <title></title>

        <script src = "http://js.nicedit.com/nicEdit-latest.js"></script>

        <script>
            window.onload = function () {
                var body = document.body;
                var limit = 1000;

                for (var i = 0; i < limit; i ++) {
                    var textarea = document.createElement ("textarea");
                        textarea.style.height = "100px";
                        textarea.style.width = "100%";

                    body.appendChild (textarea);
                }

                // The magic
                body.addEventListener ("click", function (event) {
                    var target = event.target;

                    if (target.nodeName === "TEXTAREA") {
                        var area = new nicEditor ({fullPanel : true}).panelInstance (target);

                        area.addEvent ("blur", function () {
                            this.removeInstance (target);
                        });
                    }
                }, false);
            }
        </script>

        <style>
            textarea {
                height: 100px;
                margin-bottom: 20px;
                width: 1000px;
            }
        </style>
    </head>

    <body>
        <!-- Create any textarea you want -->
    </body>
</html>

在此处输入图像描述

You can't do the trick with more then one textarea, what about 1000 ?

<!DOCTYPE html>

<html>
    <head>
        <meta charset = "utf-8">

        <title></title>

        <script src = "http://js.nicedit.com/nicEdit-latest.js"></script>

        <script>
            window.onload = function () {
                var body = document.body;
                var limit = 1000;

                for (var i = 0; i < limit; i ++) {
                    var textarea = document.createElement ("textarea");
                        textarea.style.height = "100px";
                        textarea.style.width = "100%";

                    body.appendChild (textarea);
                }

                // The magic
                body.addEventListener ("click", function (event) {
                    var target = event.target;

                    if (target.nodeName === "TEXTAREA") {
                        var area = new nicEditor ({fullPanel : true}).panelInstance (target);

                        area.addEvent ("blur", function () {
                            this.removeInstance (target);
                        });
                    }
                }, false);
            }
        </script>

        <style>
            textarea {
                height: 100px;
                margin-bottom: 20px;
                width: 1000px;
            }
        </style>
    </head>

    <body>
        <!-- Create any textarea you want -->
    </body>
</html>

enter image description here

£冰雨忧蓝° 2024-10-21 22:09:42

这就是你想要的:

<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> 

<script type="text/javascript"> 

/*
 * Replace the textarea with the given id with an instance of niceEdit.
 * Attach an 'onblur' event to the editor that removes the niceEditor when 
 * the editor loses focus, and add the onclick function to the restored textarea
 */
function toggleEditor(id){
    new nicEditor({ fullPanel: true }).panelInstance(id).addEvent('blur', function() {
        this.removeInstance(id);
        document.getElementById(id).onclick=function(){
            toggleEditor(id)
        };
    });
};

</script>

<textarea id="fieldname" cols="35" onclick="toggleEditor(this.id);">Text text</textarea>
<br><br><br>
<textarea id="fieldname2" cols="35" onclick="toggleEditor(this.id);" >Test text</textarea>

This does what you want:

<script type="text/javascript" src="http://js.nicedit.com/nicEdit-latest.js"></script> 

<script type="text/javascript"> 

/*
 * Replace the textarea with the given id with an instance of niceEdit.
 * Attach an 'onblur' event to the editor that removes the niceEditor when 
 * the editor loses focus, and add the onclick function to the restored textarea
 */
function toggleEditor(id){
    new nicEditor({ fullPanel: true }).panelInstance(id).addEvent('blur', function() {
        this.removeInstance(id);
        document.getElementById(id).onclick=function(){
            toggleEditor(id)
        };
    });
};

</script>

<textarea id="fieldname" cols="35" onclick="toggleEditor(this.id);">Text text</textarea>
<br><br><br>
<textarea id="fieldname2" cols="35" onclick="toggleEditor(this.id);" >Test text</textarea>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文