main.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. var table = document.createElement("table");
  2. table.setAttribute("cellpadding","15");
  3. table.setAttribute("border", "1");
  4. table.style.borderCollapse = "collapse";
  5. table.style.borderColor = "#ccc";
  6. document.body.appendChild(table);
  7. for (var i = 0; i < 9; i++) {
  8. table.appendChild(document.createElement("tr"));
  9. for (var j = 0; j < 9; j++) {
  10. table.children[i].appendChild(document.createElement("td"));
  11. table.children[i].children[j].innerHTML = (i + 1) * (j + 1);
  12. table.children[i].children[j].onmouseover = colorFuncOn;
  13. table.children[i].children[j].onmouseout = colorFuncOut;
  14. table.children[i].children[j].setAttribute("name",j)
  15. }
  16. }
  17. function colorFuncOn(){
  18. var i = this.getAttribute("name");
  19. for (var j = 0; j < 9; j++) {
  20. this.parentNode.parentNode.childNodes[j].childNodes[i].style.backgroundColor= "#ccc";
  21. }
  22. for (var k = 0; k < 9; k++) {
  23. this.parentNode.childNodes[k].style.backgroundColor = "#ccc";
  24. }
  25. }
  26. function colorFuncOut(){
  27. var i = this.getAttribute("name");
  28. for (var j = 0; j < 9; j++) {
  29. this.parentNode.parentNode.childNodes[j].childNodes[i].style.backgroundColor= "#fff";
  30. }
  31. for (var k = 0; k < 9; k++) {
  32. this.parentNode.childNodes[k].style.backgroundColor = "#fff";
  33. }
  34. }