动态添加div

function ajaxButton(th) {
$("#buttonDiv").remove();
var id = th.id;
  var leftpx =getAbsoluteLeftPx(id)+$("#"+id).width()+2;
  var toppx  =getAbsoluteTopPx(id)+$("#"+id).height()+2;
    newDiv=document.createElement("div");//创建div
        newDiv.style.position="absolute";//relative
        newDiv.style.backgroundColor="pink";
        //newDiv.style.height="200px";
        //newDiv.style.width="212px";
        newDiv.id="buttonDiv";
       // newDiv.style.border="#ffff00 2px solid";
        newDiv.style.top=(toppx-50)+"px";
        newDiv.style.left=leftpx+"px";
        newDiv.style.fontSize="12px";
        newDiv.style.lineHeight="25px"
        newDiv.style.padding="5px"
        newDiv.style.zIndex="100"
        newDiv.style.visibility ="visible";//hidden
        newDiv.style.opacity="0.5";
        newDiv.style.filter="alpha(opacity=50)"
       // newDiv.innerHTML="abcds";
        document.body.appendChild(newDiv);//把创建好的div插入到body节点后,否则newDiv会处于游离状态,无法显示;

}

  function getAbsoluteLeftPx(objectId)  //获取控件的绝对左起位置
{
  o = document.getElementById(objectId)
  oLeft = o.offsetLeft
  while(o.offsetParent!=null) {
oParent = o.offsetParent
oLeft += oParent.offsetLeft
o = oParent
}
   return oLeft
     }

function getAbsoluteTopPx(objid)   //获取控件的绝对上起位置
{
   o = document.getElementById(objid);
oTop = o.offsetTop;
while(o.offsetParent!=null)
{
    oParent = o.offsetParent
  oTop += oParent.offsetTop
  o = oParent
}
  return oTop
}

你可能感兴趣的:(div)