日期:2014-05-16  浏览次数:20445 次

6*6的格子,按下后分辨点的是哪一个,我搞了两段代码还是不行。
我就不适合编程。。。。。
JScript code

document.onmousedown=function(){
    var x=event.clientX;
    var y=event.clientY;
    for(var k=0;k<6;k++){
        for(var j=0;j<6;j++){
            if(((x>100*j+14)&&(x<100*j+86))&&((y>100*k+14)&&(x<100*k+86))){
                alert("范围内x:"+j+"\n"+"范围内y:"+k);break;
            }
        }
    }
}


JScript code


document.onmousedown=function(){
    var x=event.clientX;
    var y=event.clientY;
for(var j=0;j<6;j++){
if(x>100*0+14){if(x<100*0+86){alert("x:1");}}break;
if(x>100*1+14){if(x<100*1+86){alert("x:2");}}break;
if(x>100*2+14){if(x<100*2+86){alert("x:3");}}break;
if(x>100*3+14){if(x<100*3+86){alert("x:4");}}break;
if(x>100*4+14){if(x<100*4+86){alert("x:5");}}break;
if(x>100*5+14){if(x<100*5+86){alert("x:6");}}break;
}
for(var k=0;k<6;k++){
if(y>100*0+14){if(y<100*0+86){alert("y:1");}}break;
if(y>100*1+14){if(y<100*1+86){alert("y:2");}}break;
if(y>100*2+14){if(y<100*2+86){alert("y:3");}}break;
if(y>100*3+14){if(y<100*3+86){alert("y:4");}}break;
if(y>100*4+14){if(y<100*4+86){alert("y:5");}}break;
if(y>100*5+14){if(y<100*5+86){alert("y:6");}}break;
}



------解决方案--------------------
<title>无标题文档</title>
<script type="text/javascript">
function init(){
var table=document.getElementById("test");
var frag=document.createDocumentFragment;
for(var i=1;i<=6;i++){
var tr=document.createElement("tr");
for(var j=1;j<=7;j++){
var td=document.createElement("td");
td.setAttribute("ij","第"+i+"行第"+j+"列");
td.style.width="100px";
td.innerHTML=i+""+j;
td.onclick=shows;
tr.appendChild(td);
}
table.appendChild(tr);
}
}
function shows(){
alert(this.getAttribute("ij"));
}
window.onload=init;
</script>
</head>

<body>
<table id="test">
</table>
</body>
</html>
这样试试
------解决方案--------------------
HTML code

<!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=utf-8" />
<title>无标题文档</title>
<style type="text/css">
table { border:0; border-collapse:collapse; }
td { border:1px solid #999; width:30px; height:30px; }
</style>
<script type="text/javascript">
window.onload = function() {
    var obj = document.getElementsByTagName('td');
    for (var i = 0; i < obj.length; i ++) {
        obj[i].onclick = function() {
            this.style.backgroundColor = '#FF0';
            alert('x:' + (this.parentNode.rowIndex + 1) + ', y:' + (this.cellIndex + 1));
        }
    }
}
</script>
</head>

<body>
<table>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>