突出显示textarea上的边框和句柄

时间:2013-03-14 10:50:07

标签: javascript jquery html

我已经动态创建了textareas,这些textareas嵌套在动态创建的div中。 div是draggabnle,textareas可以使用jquery调整大小。

我希望任何paricular div / textarea的边框和拖动/调整大小句柄仅在用户点击该特定文本区域时出现。我假设我需要使用draggable正在使用的相同的onclick事件,但我没有使用手柄和边框显示的js知识。

任何帮助非常感谢

这是我的代码

<!DOCTYPE html>
<html>
<meta charset="utf-8" />
<head>
<style>
  body{background-color:#ccc;}
  .dragbox{width:10px; height:10px;padding: 0.0em; margin:25px; border:0;cursor:move; z-index:2}
  .textarea1{ width: 300px; height: 300px; padding: 0.5em; z-index:3}
  #handle{
      display: block; 
      height: 16px; 
      width: 100px; 
      background-color: red;
      position: absolute;
      top:-15px;
      left:0px;
      font-size:10px;
      }
     #content
        {
            position:absolute;
            top:150px;
            left:0px;
            margin:auto;
            z-index:1;
        }
</style>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" />  
<script src = "http://js.nicedit.com/nicEdit-latest.js"></script>
<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>        
var i=0;    
var p=25;   



}
function editor1(idf) {
//var body = document.body;
// The magic
document.getElementById(idf).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);
};

function NewTextArea(id)
{       
id=id+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 +"'  onDblClick='editor1("+i+")' name='textarea["+i +"]' class='textarea1' 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='"+i+"' name='id["+i+"]'><br><input name='box_type["+i+"]' type='hidden' value='text'/>"; 
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>



</head>

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

<input type="submit" value="Save Page"  >


</form>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

如果我正确理解了这个问题,可以用CSS完成:

textarea { resize:none; border:none; }
textarea:focus { resize:both; border:1px solid #000; }

JSFiddle example