2013-03-25 60 views
0

我有一個動態textarea嵌套在一個div內。 div可調整大小和拖動。 我爲div創建了一個刪除圖標,但希望刪除圖標位於div的NE角落,並在div被調整大小時移動。jquery動態定位

我已經創建div,文本區域和圖標與此代碼

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><br><textarea id='"+i +"' onDblClick='editor1("+i+")' name='textarea["+i +"]' class='textarea1' style='position:absolute; top:10px;left:0px;overflow-y: auto;background-color:transparent;border: 2px dashed #000; '>some text here"+i+"</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); 

    var but=document.createElement('input'); 
    but.setAttribute('type','button'); 
    but.setAttribute('class','removebutton');  

    but.onclick=function(){ 
     if(confirm('Really delete?')) 
     { 
     document.getElementById("frmMain").removeChild(newdiv); 
     document.getElementById(id).removeChild(but); 
     document.getElementById(id).removeChild(newbr); 
     }  
    } 

    newbr=document.createElement('BR'); 
    document.getElementById(id).appendChild(newbr); 
    document.getElementById(id).appendChild(but); 



    $(function() 
    { 
     $("#"+i).resizable({autoHide:true}) 
     $("#"+id).hover(function() { 
      $("#"+i).css('border', '2px dashed #000');   
     }); 
     $("#"+id).mouseleave(function() { 
      $("#"+i).css('border', '0px dashed #000');   
     }); 
     $("#"+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); 
    } 

    p=p+25;  
    i++; 

回答

1

使用你的按鈕的樣式。

float:right 

將您的按鈕放在div的右側。 只要確保你得到的按鈕作爲div內的第一個元素。

1

添加樣式

.dragbox { 
    position: relative; 
} 

.removebutton { 
    position: absolute; 
    top: 5px; 
    right: 5px; 
} 
+0

我想刪除東北角的圖標 – 2013-03-25 15:05:00

+0

圖標是否具有類屬性 – 2013-03-25 15:10:06

+0

是圖標有一個class but.setAttribute('class','removebutton'); – 2013-03-26 10:30:31