Javascript jquery动态定位
我有一个嵌套在div中的动态文本区域。 该div可调整大小并可拖动。 我已经为div创建了一个删除图标,但希望删除图标位于div的东北角,并在调整div大小时移动 我用这个代码创建了div、textarea和图标Javascript jquery动态定位,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有一个嵌套在div中的动态文本区域。 该div可调整大小并可拖动。 我已经为div创建了一个删除图标,但希望删除图标位于div的东北角,并在调整div大小时移动 我用这个代码创建了div、textarea和图标 function NewTextArea(id) { id=id+i; var newdiv = document.createElement('div'); newdiv.setAttribute('id', id); newd
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++;
函数NewTextArea(id)
{
id=id+i;
var newdiv=document.createElement('div');
newdiv.setAttribute('id',id);
setAttribute('class','dragbox');
newdiv.setAttribute('iterate',i);
newdiv.style.position=“相对”;
newdiv.style.top=p;
newdiv.style.left=p;
newdiv.style.cursor='move';
newdiv.innerHTML=“
此处的一些文本”+i+”;
newdiv.innerHTML=newdiv.innerHTML+“
”;
newdiv.innerHTML=newdiv.innerHTML+“
”;
newdiv.innerHTML=newdiv.innerHTML+“
”;
文件.getElementById(“frmMain”).appendChild(newdiv);
var but=document.createElement('input');
但是.setAttribute('type','button');
但是.setAttribute('class','removebutton');
但是.onclick=function(){
如果(确认('真的删除?'))
{
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);
$(函数()
{
$(“#”+i).可调整大小({autoHide:true})
$(“#”+id).悬停(函数(){
$(“#”+i).css('border','2px-smash#000');
});
$(“#”+id).mouseleave(函数(){
$(“#”+i).css('border','0px-shopped#000');
});
$(“#”+i).可调整大小(
{
停止:功能(事件、用户界面)
{
var-width=ui.size.width;
var height=ui.size.height;
//警报(“宽度=”+宽度+”高度=+高度);
比例(宽度、高度、ui.element.context.id);
}
});
$(“#”+id).可拖动(
{
停止:功能(事件、用户界面)
{
Stoppos=$(this.position();
$(“div#stop”).text(“stop:\nLeft:+Stoppos.left+”\nTop:+Stoppos.top”);
//警报(“left=“+Stoppos.left+”top=“+Stoppos.top”);
Valposition(Stoppos.left,Stoppos.top,$(this.attr('iterate'));
}
});
$(“#”+i).draggable({handle:“#handle”});
});
函数值比例(defaultwidth、defaultheight、id){
$('#width'+id).val(defaultwidth);
$('#height'+id).val(defaultheight);
}
函数valposition(defaultleft,defaulttop,id){
$('#left'+id).val(defaultleft);
$('#top'+id).val(defaulttop);
}
p=p+25;
i++;
添加样式
.dragbox {
position: relative;
}
.removebutton {
position: absolute;
top: 5px;
right: 5px;
}
在按钮上使用样式
float:right
我会把你的按钮放在div的右边。
只需确保将按钮作为div中的第一个元素。我希望删除图标位于NE角落。该图标有一个类属性。该图标有一个类,但是。setAttribute('class','removebutton');