js获取鼠标坐标,设置div的高度、位置、内容等,及注意要点
2013-12-13 22:04
766 查看
后台:GridView控件的方法
前台:javascript部分
实现功能:当鼠标悬停在、离开、移动于gridview第2列上时,展现细节内容的div层或显示、或隐藏、或移动。
注意要点(易出问题的地方):1、div1.style.display = "block"; "block"需要加引号。
2、div1.style.position = "absolute"; 这一句是需要的。
3、div1.style.left = x + "px"; 需要加上"px"。如果缺少,只能在搜狗浏览器上正常运行,起作用;在IE、FireFox、360浏览器上,div的位置将是固定不随鼠标动的。
4、var e = e || event; 这里应该是event,不应该是window.event。如果写成window.event,在FireFox上不兼容。
protected void ShowGrid_OnRowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { //string str1,......... e.Row.Cells[2].Attributes.Add("onmouseover", "mouseOverTip('" + str1+ "','" + str2+ "','" + "......"+ "')"); e.Row.Cells[2].Attributes.Add("onmouseout", "mouseOutTip()"); //e.Row.Cells[2].Attributes.Add("onmousemove", "mousemoveTip('" + str1+ "','" + str2+ "','" + "......"+ "',event)"); e.Row.Cells[2].Attributes.Add("onmousemove", "mousemoveTip2(event)"); } if (e.Row.RowType == DataControlRowType.DataRow)//-1 != e.Row.RowIndex { //鼠标经过时,行背景色变 e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'"); Button1.Text = e.Row.RowIndex.ToString(); Forecast_DataBind(((Label)(e.Row.Cells[0].Controls[1])).Text.ToString().Trim()); //鼠标移出时,行背景色变 e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'"); } }
前台:javascript部分
<script type="text/javascript" > function mouseOverTip(str1,str2,....) { var div1 = document.getElementById("tipdiv"); div1.style.display = "block"; document.getElementById("gnamediv").innerHTML = "站名:" + str1; document.getElementById("wstatusdiv").innerHTML = "天气现象:" + str2; //...... } function mouseOutTip() { var div1 = document.getElementById("tipdiv"); div1.style.display = "none"; } function mousemoveTip(str1,str2,.....,event) { var div1 = document.getElementById("tipdiv"); div1.style.display = "block"; document.getElementById("gnamediv").innerHTML = "名称:" + str1; document.getElementById("wstatusdiv").innerHTML = "天气:" + str2; //...... var x, y; var e = e || event; x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft + 20; y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; div1.style.position = "absolute"; div1.style.top = y + "px"; div1.style.left = x + "px"; } function mousemoveTip2(event) { var div1 = document.getElementById("tipdiv"); var x, y; var e = e || event; x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft + 20; y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; div1.style.position = "absolute"; div1.style.top = y + "px"; div1.style.left = x + "px"; } </script>
实现功能:当鼠标悬停在、离开、移动于gridview第2列上时,展现细节内容的div层或显示、或隐藏、或移动。
注意要点(易出问题的地方):1、div1.style.display = "block"; "block"需要加引号。
2、div1.style.position = "absolute"; 这一句是需要的。
3、div1.style.left = x + "px"; 需要加上"px"。如果缺少,只能在搜狗浏览器上正常运行,起作用;在IE、FireFox、360浏览器上,div的位置将是固定不随鼠标动的。
4、var e = e || event; 这里应该是event,不应该是window.event。如果写成window.event,在FireFox上不兼容。
相关文章推荐
- JS 获取DIV中鼠标相对位置坐标
- JS获取鼠标坐标并且根据鼠标位置不同弹出不同内容
- JS 获取DIV中鼠标相对位置的方法
- js 获取鼠标位置坐标
- js获取DIV的位置坐标的三种方法
- JS获取页面鼠标点击位置的坐标
- js获取鼠标坐标位置兼容多个浏览器
- 【JS温故知新】之——给力的鼠标坐标位置获取
- 【JS温故知新】之——给力的鼠标坐标位置获取(转)
- DIV弹出层练习(二) ------------------现有一层div想获取鼠标的光标位置并作为这个层的坐标显示出这个层
- JS获取area元素坐标和鼠标在页面中的位置
- js获取DIV的位置坐标的三种方法!
- js获取鼠标坐标位置
- 通过js获取到屏幕高度,借助屏幕高度设置div的高度
- js获取DIV的位置坐标
- js获取DIV的位置坐标的三种方法!
- 获取鼠标消息响应时的坐标位置应该注意的几个宏
- js与jquery中获取当前鼠标的x、y坐标位置的代码
- JS——获取鼠标位置(坐标)
- js 获取页面高度和宽度(兼容 ie firefox chrome),获取鼠标点击位置