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

时间:2011-01-31 19:22:17

标签: javascript html textarea nicedit

我正在使用nicedit(http://nicedit.com/index.php)。

我一直在尝试创建一个默认的html textarea,当你点击它时会变成一个nicedit文本字段。我希望它在失去焦点时恢复到简单的html textarea。当我只使用一个textarea时,我已经能够成功地做到这一点,但是,当我使用两个textareas奇怪的事情发生时(在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>

您单击并取消聚焦的第一个文本区域可以完美地工作,但是,单击的第二个文本区域在尝试取消聚焦时不会消失。我做错了什么?

2 个答案:

答案 0 :(得分:5)

你不能用超过一个textarea做什么,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

答案 1 :(得分:0)

这样做你想要的:

<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>