动态创建层提示代码

<script type="text/javascript">
function $(id)
{
return document.getElementById(id);
}
function cdiv(){
//$("idt").innerHTML="<img src='logo.jpg'/>";
if($('div1')==undefined)
{
var createDiv = document.createElement("div");
// createDiv.setAttribute("class","aaa")
createDiv.style.border = "1px solid #ccc";
createDiv.setAttribute("id","div1");
createDiv.style.background = "#f60";
createDiv.setAttribute("position","absolute");
createDiv.style.height = 10;
createDiv.style.width = 100;
createDiv.style.top =$('tlink').offsetTop;
alert($('tlink').offsetLeft+"--"+$('tlink').width+"----"+200+"---"+$('tlink').offsetWidth);
///获取文本框后面的坐标
createDiv.style.left =$('tlink').offsetLeft+3+$('tlink').offsetWidth;
createDiv.style.position ="absolute";
//var createText = document.createTextNode("我是一个层");
createDiv.innerHTML="<a href='#'>55555</a>"
//var img = new Image();
//img.width =1;
//img.height=10;
//img.src="logo.jpg";
//createDiv.appendChild(img);
//createDiv.appendChild(createText);

var tlink = document.getElementById("tlink");
var parent = tlink.parentNode;
if(parent.lastChild == tlink) {
parent.appendChild(createDiv);
} else {
parent.insertBefore(createDiv, tlink.nextSibling);
}

}
}


window.onload = function(){
tlink.onclick=function()
{
cdiv();
}
}
</script>
</head>
<input type="text" id="tlink" width="60px">

你可能感兴趣的:(JavaScript)