javascript动态textarea和nicedit

时间:2013-03-11 09:01:01

标签: javascript html onclicklistener nicedit

在这里的几个家伙的帮助下,我设法创建了一个动态可调整大小/可拖动的textareas的页面。我现在正试图将nicedit归入这些textareas。它的工作到了一定程度。双击textarea成为一个nicedit区域,但不幸的是,可拖动事件甚至覆盖了nicedit,因此无法编辑textarea。

我的javascript是有限的所以我希望有人可以指出我的方式错误

提前感谢。

这里是jsfiddle链接http://jsfiddle.net/JVhpJ/9/

继承人的代码

<!DOCTYPE html>
<html>
<head>
    <meta charset = "utf-8">       
    <script src = "http://js.nicedit.com/nicEdit-latest.js"></script>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />  
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>   
    <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script>  
    <script>
        window.onload = function () {
            var body = document.body;
            // The magic
            body.addEventListener ("dblclick", 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);
        }
    var i=0;    
    var p=25;   
    function creatediv1(id)
    {   
        id=id+i;
        var xp=xp+i;
        var newdiv = document.createElement('div');
        newdiv.setAttribute('id', id);
        newdiv.setAttribute('class', 'dragbox');
        newdiv.setAttribute('iterate',i);
        newdiv.style.position = "relative";
        newdiv.style.top = p;
        newdiv.style.left = p;
        newdiv.style.cursor='move';
        newdiv.innerHTML = "<div id='handle'>Drag me into position</div></div><br><textarea id="+i +" name='textarea["+i +"]' class='textarea1' width='300' style='position:absolute; top:0px;left:0px;overflow-y: auto;background-color:transparent;border: 2px dashed #000; '>some text here</textarea>";
        newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='300' name='width["+i+"]' id='width"+i+"'><br><input type='hidden' value='300' name='height["+i+"]' id='height"+i+"'>";               
        newdiv.innerHTML=newdiv.innerHTML+"<br><input type='hidden' value='0' name='left["+i+"]' id='left"+i+"'><br><input type='hidden' value='0' name='top["+i+"]' id='top"+i+"'>";           

        document.getElementById("frmMain").appendChild(newdiv);
        $(function()
        {

            $("#"+i).resizable(
            {
                stop: function(event, ui)
                {
                    var width = ui.size.width;
                    var height = ui.size.height;
                   // alert("width="+width+"height="+height);
                    ValProportions(width,height,ui.element.context.id);           
                }
            });

            $( "#"+id ).draggable(
            {
                stop: function(event, ui)
                {
                    Stoppos = $(this).position();
                   $("div#stop").text("STOP: \nLeft: "+ Stoppos.left + "\nTop: " + Stoppos.top);
                       // alert("left="+Stoppos.left+"top="+Stoppos.top);
                    ValPostion(Stoppos.left,Stoppos.top,$(this).attr('iterate'));   
                }
            }); 
             $("#"+i).draggable({handle:"#handle"});  
        });  

        function ValProportions(defaultwidth, defaultheight,id)  { 
            $('#width'+id).val(defaultwidth);
            $('#height'+id).val(defaultheight);
            }
        function ValPostion(defaultleft,defaulttop,id)  {  
            $('#left'+id).val(defaultleft);
            $('#top'+id).val(defaulttop);
            }
        i++;
        p=p+25; 

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

<body>
   <form id="frmMain" name="frmMain" enctype="multipart/form-data" action="test.php" method="post">
    <input id="btn1" type="button" value="Add New textbox" onclick="creatediv1('draggable');" />             
    <input type="submit" value="Submit"  >
   </form>
</body>

1 个答案:

答案 0 :(得分:0)

我设法通过在div的mousedown事件上使用stoppropagation()修复此问题,这使我可以编辑文本而不会拖动覆盖编辑功能