我正在学习
JavaScript,最近我一直在尝试鼠标事件,试图了解它们的工作原理.
<html> <head> <title>Mouse Events Example</title> <script type="text/javascript"> function handleEvent(oEvent) { var oTextBox = document.getElementById("txt1"); oTextBox.value += "\n" + oEvent.type; if(oEvent.type=="click") { var iScreenX = oEvent.screenX; var iScreenY = oEvent.screenY; var b = "Clicked at "+iScreenX+","+iScreenY; alert(b); } } function handleEvent1(oEvent) { // alert("Left Window"); } </script> </head> <body> <p>Use your mouse to click and double click the red square</p> <div style="width: 100px; height: 100px; background-color: red" onmouSEOver="handleEvent(event)" onmouSEOut="handleEvent1(event)" onmousedown="handleEvent(event)" onmouseup="handleEvent(event)" onclick="handleEvent(event)" ondblclick="handleEvent(event)" id="div1"></div> <p><textarea id="txt1" rows="15" cols="50"></textarea></p> </body>
这是我一直试图理解的代码.任何人都可以帮我创建一个HTML表格,点击表格的单元格用户被告知单元格,他正在点击?
被困在它的时间,谢谢帮助.
解决方法
var table = document.getElementById("tableID"); if (table != null) { for (var i = 0; i < table.rows.length; i++) { for (var j = 0; j < table.rows[i].cells.length; j++) table.rows[i].cells[j].onclick = function () { tableText(this); }; } } function tableText(tableCell) { alert(tableCell.innerHTML); }
是你可以做的一个例子. DEMO