为什么我的覆盖功能在我的浏览器中不起作用

时间:2013-03-27 04:08:44

标签: php javascript html

我使用谷歌浏览器作为我的浏览器 这是我的网页来源

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>

<script type="text/javascript">
function createOverlay() {
    var div = document.createElement("div");
    div.innerHTML = "your own code here";
    div.id = "overlay";
    div.display = "none";
    div.style.position = "absolute";
    div.style.border = "1px solid";
    document.body.appendChild(div);
}

//this function show layout
function showOverlay(el) {
    if (!document.getElementById("overlay")) createOverlay();
    var div = document.getElementById("overlay");
    div.style.display = "block";
    div.style.width = "200px";
    div.style.height = "200px";
    div.style.backgroundColor="FFFF"
    div.style.top = el.offsetTop + 40 + "px";
    div.style.left = parseInt(el.offsetLeft) + parseInt(div.style.width) / 2 + "px";
    div.onmouseover = function (event) {
        event.stopPropagation();
        return false;
    };
}

// and this function hide - you can use it for "close" button on overlay
function hideOverlay() {
    document.getElementById("overlay").style.display = "none";
}

//here is the code which add event handlers to td elements of your table
var table = document.getElementsByTagName("table")[0];
var tds = table.getElementsByTagName("td");
var flag = true;
if (document.getElementById("overlay")) {
    if (document.getElementById("overlay").style.display != "none") flag = false;
}
for (i in tds) {
    tds[i].onmouseover = function (event) {
        if (flag) {
            showOverlay(this);
            event.stopPropagation();
        } else {
            return false;
        }
    };
}
</script>
<body>
<div>
<table width="1023" height="248" border="1">
  <tr>
    <th colspan="2" scope="col">A1</th>
    <th colspan="2" scope="col">A2</th>
    <th colspan="2" scope="col">A3</th>
    <th colspan="2" scope="col">A4</th>
    <th colspan="2" scope="col">A5</th>
    <th colspan="2" scope="col">A6</th>
    <th colspan="2" scope="col">A7</th>
    <th colspan="2" scope="col">A8</th>
    <th colspan="2" scope="col">A9</th>
  </tr>
  <tr>
    <td><div align="center">A1.4</div></td>
    <td><div align="center">A1.8</div></td>
    <td><div align="center">A2.4</div></td>
    <td><div align="center">A2.8</div></td>
    <td><div align="center">A3.4</div></td>
    <td><div align="center">A3.8</div></td>
    <td><div align="center">A4.4</div></td>
    <td><div align="center">A4.8</div></td>
    <td><div align="center">A5.4</div></td>
    <td><div align="center">A5.8</div></td>
    <td><div align="center">A6.4</div></td>
    <td><div align="center">A6.8</div></td>
    <td><div align="center">A7.4</div></td>
    <td><div align="center">A7.8</div></td>
    <td><div align="center">A8.4</div></td>
    <td><div align="center">A8.8</div></td>
    <td><div align="center">A9.4</div></td>
    <td><div align="center">A9.8</div></td>
  </tr>
  <tr>
    <td><div align="center">A1.3</div></td>
    <td><div align="center">A1.7</div></td>
    <td><div align="center">A2.3</div></td>
    <td><div align="center">A2.7</div></td>
    <td><div align="center">A3.3</div></td>
    <td><div align="center">A3.7</div></td>
    <td><div align="center">A4.3</div></td>
    <td><div align="center">A4.7</div></td>
    <td><div align="center">A5.3</div></td>
    <td><div align="center">A5.7</div></td>
    <td><div align="center">A6.3
    </div>
    <div align="center"></div></td>
    <td><div align="center">A6.7</div></td>
    <td><div align="center">A7.3</div></td>
    <td><div align="center">A7.7</div></td>
    <td><div align="center">A8.3</div></td>
    <td><div align="center">A8.7</div></td>
    <td><div align="center">A9.3</div></td>
    <td><div align="center">A9.7</div></td>
  </tr>
  <tr>
    <td><div align="center">A1.2</div></td>
    <td><div align="center">A1.6</div></td>
    <td><div align="center">A2.2</div></td>
    <td><div align="center">A2.6</div></td>
    <td><div align="center">A3.2</div></td>
    <td><div align="center">A3.6</div></td>
    <td><div align="center">A4.2</div></td>
    <td><div align="center">A4.6</div></td>
    <td><div align="center">A5.2</div></td>
    <td><div align="center">A5.6</div></td>
    <td><div align="center">A6.2</div></td>
    <td><div align="center">A6.6</div></td>
    <td><div align="center">A7.2</div></td>
    <td><div align="center">A7.6</div></td>
    <td><div align="center">A8.2</div></td>
    <td><div align="center">A8.6</div></td>
    <td><div align="center">A9.2</div></td>
    <td><div align="center">A9.6</div></td>
  </tr>
  <tr>
    <td><div align="center">A1.1</div></td>
    <td><div align="center">A1.5</div></td>
    <td><div align="center">A2.1</div></td>
    <td><div align="center">A2.5</div></td>
    <td><div align="center">A3.1</div></td>
    <td><div align="center">A3.5</div></td>
    <td><div align="center">A4.1</div></td>
    <td><div align="center">A4.5</div></td>
    <td><div align="center">A5.1</div></td>
    <td><div align="center">A5.5</div></td>
    <td><div align="center">A6.1</div></td>
    <td><div align="center">A6.5</div></td>
    <td><div align="center">A7.1</div></td>
    <td><div align="center">A7.5</div></td>
    <td><div align="center">A8.1</div></td>
    <td><div align="center">A8.5</div></td>
    <td><div align="center">A9.1</div></td>
    <td><div align="center">A9.5</div></td>
  </tr>
</table>
</div>
</body>
</html>

当我在演示中尝试http://jsfiddle.net/andricoga/yTj8K/时,它的工作正常。但是当我在本地主机上尝试时,没有任何改变只显示一个表。我的java脚本不起作用。 任何想法是什么问题。请指教

1 个答案:

答案 0 :(得分:2)

在执行之前,您需要检查 DOM 已加载

//here is the code which add event handlers to td elements of your table
var table = document.getElementsByTagName("table")[0];
var tds = table.getElementsByTagName("td");
var flag = true;
if (document.getElementById("overlay")) {
    if (document.getElementById("overlay").style.display != "none") flag = false;
}
for (i in tds) {
    tds[i].onmouseover = function (event) {
        if (flag) {
            showOverlay(this);
            event.stopPropagation();
        } else {
            return false;
        }
    };
}

所以要么在onload事件之后执行它。或者移动脚本的这一部分,以便在呈现<table>之后显示。

您可以使用jQuery $(document).ready(function() { ... });。或者非jQuery解决方案,请参阅:Dom loaded event cross borwser native javascript code

它适用于jsfiddle,因为脚本被指定为执行 onload