动态创建Div层

ws_hgo 2009-09-20 07:47:38
下面是个表格
编号 姓名
2 ws_hgo2
3 ws_hgo3
4 ws_hgo4
当我的鼠标划到ws_hgo2的时候
在ws_hgo2上面就有个Div层,里面有图片,描述等信息
鼠标离开的时候,Div层消失

同理鼠标划到ws_hgo3,ws_hgo4一样!


...全文
263 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuzhilon88 2012-05-23
  • 打赏
  • 举报
回复
<script language=javascript>
function f_creatediv(divcnt){
for(var i=0;i<divcnt;i++){
var objdiv = document.createElement("DIV");
var objname="shop_" + i
objdiv.id = objname;
objdiv.style.top = 100 * i + 100;
objdiv.style.left = 100 * i + 100;
objdiv.style.background = '#FFFF00';
objdiv.style.visibility = 'visible';
objdiv.style.width = 100;
objdiv.style.height = 80;
objdiv.style.border = "5 groove black";
objdiv.innerHTML="SHOP_" + i;
document.body.appendChild(objdiv);
document.getElementById(objname).onmouseover = function()
{
alert(this.id);
};
}
}
</script>
<input type="button" onclick="f_creatediv(4)">
yuxia1314741 2009-09-29
  • 打赏
  • 举报
回复
up
kaizi_sun 2009-09-22
  • 打赏
  • 举报
回复
   
function showTip(oEvent){
var oDiv = document.getElementById("divTip1");
oDiv.style.visibility="visible";
oDiv.style.left = oEvent.clientX-25;
oDiv.style.top =oEvent.clientY-35;
}
function hideTip(oEvent){
var oDiv = document.getElementById("divTip1");
oDiv.style.visibility = "hidden";
} }

<body>
<p>Move your mouse over the red square. </p>
<div id="div1"
style="background-color:Red;height:50px;width:50px"
onmouseover="showTip(event)" onmouseout="hideTip(event)"></div>
<div id="divTip1"
style="background:yellow; position:absolute; visibility:hidden; padding: 5px">
<span style="font-weight:bold">Custom Tooltip</span><br /></div>
</body>

divTip1里面内容自己动态绑定。这个是书上的例子,看下合适你的需求吗
ws_hgo 2009-09-22
  • 打赏
  • 举报
回复
我的那个值根据鼠标滑过去的值传给后台
根据值获取信息返回给
Div的innerHTML

但是上面
我测试了的达不到相应的效果
感觉写复杂啦
ws_hgo 2009-09-22
  • 打赏
  • 举报
回复
UP
xinyung 2009-09-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 ws_hgo 的回复:]
谢谢7楼的代码
还是有问题
当我鼠标离开的时候Div层并没有消失
[/Quote]
IE FF下可以消失
ws_hgo 2009-09-21
  • 打赏
  • 举报
回复
UP
ziyuanxian 2009-09-21
  • 打赏
  • 举报
回复
因为没有onmouseout
ws_hgo 2009-09-21
  • 打赏
  • 举报
回复
谢谢7楼的代码
还是有问题
当我鼠标离开的时候Div层并没有消失
mingfish 2009-09-21
  • 打赏
  • 举报
回复
路过,超强的代码啊
yw1386 2009-09-21
  • 打赏
  • 举报
回复
学习了
ws_hgo 2009-09-21
  • 打赏
  • 举报
回复
感谢1楼兄弟写的代码
我的Div层是在鼠标划过的地方上面显示Div而不是右边
xinyung 2009-09-21
  • 打赏
  • 举报
回复
更改一下位置就行了,借用1楼代码(未判断边界)
<style>
#otbl {
border: 1px solid #8F8F8F;
border-collapse: collapse;
}
#otbl td {
border: 1px solid #8F8F8F;
width: 60px;
height: 30px;
}
#odiv {
width:200px;
height:100px;
border: 2px solid #D0D0D0;
background-color:#fff;
position:absolute;
display:none;
}

</style>
<script type="text/javascript">
<!--
var $ = function(id) { return "string" == typeof id ? document.getElementById(id) : id };
var CancelBubble = function(e){
var e = e || window.event;
try{
e.cancelBubble = true;
}catch(ex){
try{
e.stopPropagation();
}catch(e){}
}
}

var getTarget = function(e){
return e.srcElement || e.target;
}

var absPosition = function(element){
var el = element;
var iTop = iLeft = 0;
do{
iTop += element.offsetTop;
iLeft += element.offsetLeft;
}while(element = element.offsetParent);
return {'x': iLeft - $("odiv").offsetWidth/2 - el.offsetWidth/2, 'y': iTop - $("odiv").offsetHeight};
}
function showTip(e, obj){
CancelBubble(e);
var e = e || window.event
var otarget = getTarget(e);
$("odiv").style.display = "block";

with($("odiv").style){
display="block";top=absPosition(otarget).y+"px";left=(absPosition(otarget).x+otarget.offsetWidth)+"px";
}
$("odiv").innerHTML = obj.innerText || obj.textContent;
document.onmouseover = function(){
$("odiv").style.display = "none";
document.onmouseover = null;
}
$("odiv").onmouseover = function(e){
var e = e || window.event;
CancelBubble(e);
}

}
//-->
</script>
<br/><br/><br/><br/><br/><br/>
<table id="otbl" align="center">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td onmouseover="showTip(event, this)">ws_hgo2 </td>
</tr>
<tr>
<td>3</td>
<td onmouseover="showTip(event, this)">ws_hgo3</td>
</tr>
<tr>
<td>4</td>
<td onmouseover="showTip(event, this)">ws_hgo4</td>
</tr>
</tbody>
</table>

<div id="odiv">

</div>
KK3K2005 2009-09-20
  • 打赏
  • 举报
回复
基本每天都可以见到一个这样的问题
s_liangchao1s 2009-09-20
  • 打赏
  • 举报
回复

<style>
#otbl {
border: 1px solid #8F8F8F;
border-collapse: collapse;
}
#otbl td {
border: 1px solid #8F8F8F;
width: 60px;
height: 30px;
}
#odiv {
width:200px;
height:100px;
border: 2px solid #D0D0D0;
background-color:#fff;
position:absolute;
display:none;
}

</style>
<script type="text/javascript">
<!--
var $ = function(id) { return "string" == typeof id ? document.getElementById(id) : id };
var CancelBubble = function(e){
var e = e || window.event;
try{
e.cancelBubble = true;
}catch(ex){
try{
e.stopPropagation();
}catch(e){}
}
}

var getTarget = function(e){
return e.srcElement || e.target;
}

var absPosition = function(element){
var iTop = iLeft = 0;
do{
iTop += element.offsetTop;
iLeft += element.offsetLeft;
}while(element = element.offsetParent);
return {'x': iLeft, 'y': iTop};
}
function showTip(e, obj){
CancelBubble(e);
var e = e || window.event
var otarget = getTarget(e);
$("odiv").style.display = "block";

with($("odiv").style){
display="block";top=absPosition(otarget).y+"px";left=(absPosition(otarget).x+otarget.offsetWidth)+"px";
}
$("odiv").innerHTML = obj.innerText || obj.textContent;
document.onmouseover = function(){
$("odiv").style.display = "none";
document.onmouseover = null;
}
$("odiv").onmouseover = function(e){
var e = e || window.event;
CancelBubble(e);
}

}
//-->
</script>

<table id="otbl">
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
</tr>
</thead>
<tbody>
<tr>
<td>2</td>
<td onmouseover="showTip(event, this)">ws_hgo2 </td>
</tr>
<tr>
<td>3</td>
<td onmouseover="showTip(event, this)">ws_hgo3</td>
</tr>
<tr>
<td>4</td>
<td onmouseover="showTip(event, this)">ws_hgo4</td>
</tr>
</tbody>
</table>

<div id="odiv">

</div>

87,907

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧