使onClick功能可以切换textarea的位置

时间:2013-01-11 18:03:21

标签: jquery

我有以下代码:

      var counter=0; 
     function appendText(){     
     var text = $("#text").val();
if ($("#text").val()){      
var textArea = "<div> <textarea id='t"+counter+"'>"+text+
    "</textarea><button id='b"+counter+"' name='t"+counter+"' >edit</button> <button onClick='moveUp();'>Up</button></div>"; 


    $("#addedText").after(textArea);    
    $("#t"+counter).attr('readonly','readonly');    
    $("#t"+counter).attr('readonly','readonly');

    $("#t"+counter).css('width','500px');
$("#t"+counter).css('resize','none');
$("#t"+counter).css('background-color','#5CCCCC'); 
$("#t"+counter).css('font-weight','bold');             

$("#b"+counter).bind('click',makeAreaEditable);                  
counter++;  }   else{   }            };                                                                 


 var makeAreaEditable = function() {
 var target = event.target||event.srcElement||event.originalTarget;     
 var targetArea =$("#"+target.name);    
 targetArea.removeAttr('readonly');     
 var button =  $("#"+target.id);    
 button.text("OK");     
 button.unbind('click');
 button.bind('click',saveEdit); };

  var saveEdit =  function() {  
  var target = event.target || event.srcElement || event.originalTarget;    
  var button =  $("#"+target.id);   
  button.text("edit");  
  button.unbind('click');
  button.bind('click',makeAreaEditable);    
  var targetArea = $("#"+target.name);  
  targetArea.attr('readonly', 'readonly'); };

我无法弄清楚如何制作moveUp()函数以便将textarea移动到一个空格。之前的textarea应该用您按下向上按钮的位置切换位置。

html看起来像这样:

 <textarea id="text" placeholder="baga text aici"></textarea> <button
 id="addText" onClick="appendText()" >Add text </button>        <div
 id="addedText" style="float:left"></div>

1 个答案:

答案 0 :(得分:0)

$(".moveup").on("click",function()   {
       var $divcontainer = $(this).parent();            
      if ( $divcontainer.prev().get(0))  $divcontainer.prev().before($divcontainer);

      });
//append whatever how many textarea & its up button in div.allthetextarea

HTML

 <div class="allthetextarea">
      <div><textarea/><input class="moveup"/></div>
      <div><textarea/><input class="moveup"/></div>
   ...
</div>

CSS

      .allthetextarea .moveup:first-child {display:none;}