gpt4 book ai didi

javascript - document.getElementByID 正在用 html 代码替换 javascript

转载 作者:行者123 更新时间:2023-12-03 07:16:19 24 4
gpt4 key购买 nike

我有HTML id 为“containerTitle”的 h2,如下所示:

 <h2 id = "containerTitle"> Container Placeholder</h2>

在我的 JavaScript 中,我尝试替换与此 id 关联的文本,如下所示:

document.getElementById("containerTitle").innerHTML = "NewText";

但是,它坏了。当我在浏览器中查看新 HTML 的源代码时,我用 getElementById 编写的行改为:

<h2 id = "containerTitle"> Container Placeholder</h2>

它破坏了我的 JavaScript。为什么这一行要用 HTML 替换我的 JavaScript?通过 ID 获取元素对于其他一切都有效。请帮忙!我主要是一名 C# 程序员,但正在帮助处理一个 HTML 项目。

    <html>
<head>
<title> Case Scenario</title>
<style>
table tr td
{
border:1px solid #222;
padding: 0.5em;
padding-top: 0;
}
table
{
border-spacing: 0;
border-collapse: collapse;
}
body
{
background-color: rgb(250, 250, 250);
font-family: Georgia, Charcoal, serif;
font-size: 16px;
}
img
{
display: inline;
}
</style>
</head>
<body>
<h1 id = "caseTitle"> Case Scenario 1</h1>
<h2 id = "containerTitle"> Container Placeholder</h2>
<div style="border-right:1px solid #222; float:left; display:inline-
block; width:45%; overflow-y:scroll; max-height:600px; padding:1%">
<div id="caseS" style="display:inline-block; vertical-align:top;
margin-left:1%; margin-top:1%"></div>
<img id="imgS" src="" style="">
<table id="tableS" style="margin-left:10%; margin-bottom:2%;
text-align:center">
<p id="containerDescription"> Container Description placeholder</p>
</table>
</div>
<div style="margin-left:4px; display:inline-block; max-width:50%; overflow-y:scroll; max-height:600px; padding:1%;">
<div id="runningCount">Score: 0</div>
<h2 id="titleQ">Question 1</h2>
<div id="askingQ"></div>
<br>
<form id="formQ">

</form>
<input type="button" onClick="testQ()" value="Check Answer" id="checkQ">
<br>
<b id="alertQ" style="margin-top:0; padding-top:0; width:30%"></b>
</div>
<br>
<br>

<script type="text/javascript">

var CaseCount = 0;
var CaseContainerCount = 0;
var CaseContainerQuestionCount = 0;
var CaseContainerAnswerCount = 0;
var QuestionCount = 1;
var currentAnswerCollection = "Case" + CaseCount + "Container" + CaseContainerCount + "Question" + CaseContainerQuestionCount + "Answers";
var newHTML = "";
var radioArray = new Array();
var answerCount = 0;
var checkedAnswerCount = 0;


//document.write(currentAnswerCollection.length);
setup();

function setup()
{

for(var j = 0; j < AnswersArray[0].length; j++)
{

newHTML += "<span id=\"a_a"+ j + "\"></span><input type=\"radio\" name=\"grouping\" id=\"a" + j + "0\" value=\"" + j + "\"><span onclick=\"easyRadio('a" + j + "')\" ontouchstart\"easyRadio('a" + j + "')\">" + AnswersArray[answerCount][j].answerText + "</span><br><br>";

}

//}
//document.getElementById("askingQ").innerHTML = Case0Container0Questions[0];
document.getElementById("askingQ").innerHTML = QuestionsArray[CaseContainerCount][CaseContainerQuestionCount];
document.getElementById("formQ").innerHTML = newHTML;
//answerCount++;
//document.write(AnswersArray[0][0].answerText);
}

function testQ()
{

radioArray = document.getElementsByName("grouping");
for(var i = 0; i < radioArray.length; i++)
{

//document.write(i);
if(radioArray[i].checked)
{
if(AnswersArray[answerCount][i].isCorrect == "True")
{
document.getElementById("a_a" + i).innerHTML = "<img src='img/right.png' class='mark'>";
checkedAnswerCount++;
MarkRightAnswer();
break;
}
else
{
if(checkedAnswerCount < (radioArray.length - 2))
{
document.getElementById("a_a" + i).innerHTML = "<img src='img/wrong.png' class='mark'>";
//console.log(checkedAnswerCount);
checkedAnswerCount++;
}
else
{
document.getElementById("a_a" + i).innerHTML = "<img src='img/wrong.png' class='mark'>";
MarkRightAnswer();
}
break;
}

}

}
}

function MarkRightAnswer()
{
checkedAnswerCount = 0;
for(var i = 0; i < AnswersArray[answerCount].length; i++)
{
if(AnswersArray[answerCount][i].isCorrect == "True")
{
document.getElementById("a_a" + i).innerHTML = "<img src='img/right.png' class='mark'>";
//change button for next question
document.getElementById("checkQ").setAttribute("value","Next!");
document.getElementById("checkQ").setAttribute("onclick","NextQuestion()");

}

}
}

function NextQuestion()
{
CaseContainerQuestionCount++;
answerCount++;
QuestionCount++;
newHTML = "";
for(var j = 0; j < AnswersArray[0].length; j++)
{
newHTML += "<span id=\"a_a"+ j + "\"></span><input type=\"radio\" name=\"grouping\" id=\"a" + j + "0\" value=\"" + j + "\"><span onclick=\"easyRadio('a" + j + "')\" ontouchstart\"easyRadio('a" + j + "')\">" + AnswersArray[answerCount][j].answerText + "</span><br><br>";

}
//check length of containerQuestion array. If at end of array, go to next container

if(CaseContainerQuestionCount >= QuestionsArray[CaseContainerCount].length)
{
CaseContainerCount++;
CaseContainerQuestionCount = 0;
console.log( QuestionsArray[CaseContainerCount].length);
}

document.getElementById("containerTitle").innerHTML = "BOOOOOYAAA";
document.getElementById("askingQ").innerHTML = QuestionsArray[CaseContainerCount][CaseContainerQuestionCount];
document.getElementById("formQ").innerHTML = newHTML;
document.getElementById("titleQ").innerHTML = "Question " + QuestionCount;
document.getElementById("checkQ").setAttribute("value","Check Answer");
document.getElementById("checkQ").setAttribute("onclick","testQ()");
}



</script>

最佳答案

会有重复。

请检查您是否在页面中的任何位置使用“containerTitle”ID。如果你对多个 DOM 使用相同的 id,它将不起作用

关于javascript - document.getElementByID 正在用 html 代码替换 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36408533/

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