我制作了一个Sudoku表,其中带有从XML文件中预加载的一些数字。我还有另一个带有完整拼图数字的XML文件,并希望对照XML文件中的数字检查输入的数字,数字的顺序应相同。
我尝试使用document.getElementsByTagName(“ td”)。value来获取输入值,但这种方式我一无所获。
这是我的桌子。
<table id="grid">
<tr id="one">
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
</tr>
<tr id="two">
<td class="number"></td>
<td class="numer1"></td>
<td class="numer2"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
</tr>
<tr id="three">
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="numer2"></td>
</tr>
<tr id="four">
<td class="number"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="number"></td>
</tr>
<tr id="five">
<td class="numer2"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="numer2"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="numer2"></td>
<td class="number"></td>
<td class="numer1"></td>
</tr>
<tr id="six">
<td class="numer1"></td>
<td class="numer2"></td>
<td class="numer2"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="number"></td>
</tr>
<tr id="seven">
<td class="number"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="numer2"></td>
</tr>
<tr id="eight">
<td class="number"></td>
<td class="numer1"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="number"></td>
<td class="numer1"></td>
<td class="number"></td>
</tr>
<tr id="nine">
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="number"></td>
<td class="numer2"></td>
<td class="numer1"></td>
<td class="numer2"></td>
<td class="number"></td>
</tr>
这就是我用JS尝试过的
function hint(){
if (document.getElementById("easyS").value == "easy1" ||
document.getElementById("hardS").value == "hard1"){
var request1 = new XMLHttpRequest();
request1.open("GET", "puzzle1.xml", false);
request1.send(null);
var xmldoc = request1.responseXML;
var numbers1 = xmldoc.getElementsByTagName("number");
var numArray1 = [];
for(var i = 0; i < numbers1.length; i++){
numArray1.push(numbers1[i].firstChild.data)
}
var printNum1 = document.getElementsByTagName("td")[0].value;
x = printNum1.length;
console.log(printNum1);
if (printNum[x] !== numArray1[x]){
alert("wrong number");
}
}
console.log(hint());
}
这下一段代码确实起作用,并尝试执行类似的操作但无法获得它:
function solvePuzzle(){
var request1 = new XMLHttpRequest();
request1.open("GET", "puzzle1.xml", false);
request1.send(null);
var xmldoc = request1.responseXML;
var numbers1 = xmldoc.getElementsByTagName("number");
var numArray1 = [];
for(var i = 0; i < numbers1.length; i++){
numArray1.push(numbers1[i].firstChild.data)
}
var printNum1 = document.getElementsByTagName("td");
x = printNum1.length;
while(x--){
printNum1[x].innerHTML = numArray1[x];
}