gpt4 book ai didi

javascript - document.getElementById().innerHTML 不起作用,控制台上没有错误

转载 作者:行者123 更新时间:2023-11-28 06:52:44 24 4
gpt4 key购买 nike

在函数clueClicked中,我试图从数组中提取值并更改表的innerHTML,这在某种程度上不起作用。控制台上没有错误。请帮忙。我正在使用<td>的id ,更改元素的内容。

<!DOCTYPE html>
<html>
<head>
<title>Cross word Puzzel</title>
<style>
#puzzel {
width:330px;
margin: 0;
padding: 0;
border-collapse: collapse;
border: 1px solid black;
}
#cross{
text-align: center;
width:30px;
height:30px;
margin: 0;
padding: 0;
border-collapse: collapse;
border: 1px solid white;
}


#buttons{
width:30%;
float: right;

}
tr{
margin: 0;
padding: 0;
border-collapse: collapse;
}
#leftBox{
float: left;
}
#rightBox{
float: left;
}
.butt{
height:50px;
width: 100px;
}
#puzzel{
text-align: center;
}
.tableBox{
width: 30px;
height:30px;
border: 1px solid black;
text-align: center;
}
</style>

<script>

var currentTextInput;
var puzzelArrayData;

function initializeScreen(){

var puzzelTable = document.getElementById("puzzel");
puzzelArrayData = preparePuzzelArray();

for ( var i = 0; i < puzzelArrayData.length ; i++ ) {
var row = puzzelTable.insertRow(-1);
var rowData = puzzelArrayData[i];
for(var j = 0 ; j < rowData.length ; j++){
var cell = row.insertCell(-1);
if(rowData[j] != 0){
var txtID = String('txt' + '_' + i + '_' + j);
cell.innerHTML = '<input type="text" class="tableBox" maxlength="1" style="text-transform: lowercase" ' + 'id="' + txtID + '" onfocus="textInputFocus(' + "'" + txtID + "'"+ ')">';
}else{
cell.innerHTML = "1";
cell.style.backgroundColor = "black";
}
}
}

}

function textInputFocus(txtID123){
currentTextInput = txtID123;
}

function preparePuzzelArray(){
var items = [ [0, 0, 0, 0, 'p', 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 'u', 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 'n', 0, 'b', 0, 0, 0, 0],
[0, 'h', 'y', 'd', 'e', 'r', 'a', 'b', 'a', 'd', 0],
[0, 0, 0, 0, 0, 0, 'n', 0, 0, 'e', 0],
[0, 0, 0, 0, 0, 0, 'g', 0, 0, 'l', 0],
[0, 0, 'm', 'u', 'm', 'b', 'a', 'i', 0, 'h', 0],
[0, 0, 0, 0, 0, 0, 'l', 0, 0, 'i', 0],
[0, 0, 0, 0, 0, 0, 'o', 0, 0, 0, 0],
['k', 'a', 's', 'h', 'm', 'i','r', 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 'e', 0, 0, 0, 0]
];
return items;
}
function solveClicked(){
var testString = 'txt_12_15';
var tokens = testString.split('_');
alert(tokens);
}

function clearAllClicked(){
currentTextInput = '';
var puzzelTable = document.getElementById("puzzel");
puzzelTable.innerHTML = '';
initializeScreen();
}
function checkClicked(){

for ( var i = 0; i < puzzelArrayData.length ; i++ ) {
var rowData = puzzelArrayData[i];
for(var j = 0 ; j < rowData.length ; j++){
if(rowData[j] != 0){
var providedValue = document.getElementById('txt' + '_' + i + '_' + j).value;
if(providedValue != puzzelArrayData[i][j]){
console.log('Wrong entry found: ' + providedValue + " at " + i + j);
}
}
}
}
}

function clueClicked(){
var temp1 = currentTextInput;
var token = temp1.split("_");
var row = token[1];
var column = token[2];
document.getElementById(temp1).innerHTML = puzzelArrayData[row][column];//here is the problem.



}
</script>
</head>
<body onload="initializeScreen()">
<div id="leftBox">
<table id="puzzel">

</table>
</div>
<div id="rightBox">
<table>
<tr><td><input class="butt" type="submit" value="Clear All" onclick="clearAllClicked()"></td></tr>
<tr><td><input class="butt" type="submit" value="Check" onclick="checkClicked()"></td></tr>
<tr><td><input class="butt" type="submit" value="Solve" onclick="solveClicked()"></td></tr>
<tr><td><input class="butt" type="submit" value="Clue" onclick="clueClicked()"></td></tr>
</table>

</body>
</html>

最佳答案

这可能是因为您尝试在输入元素上使用innerHTML。您应该尝试使用 value 属性。 innerHTML 通常用于包含某些内容的元素,即 div、span 等...

关于javascript - document.getElementById().innerHTML 不起作用,控制台上没有错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32814135/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com