document.getElementsById(“”)。innerHTML不会更改p标记

时间:2017-04-08 00:51:31

标签: javascript html css django for-loop

HTML

{% load static %}

 <link rel="stylesheet" type="text/css" href="{% static 'lessons/style.css' %}" />
<script>
function openNav() {
    document.getElementById("mySidenav").style.width = "20%";
    document.getElementById("main").style.marginLeft = "20%";
}

function closeNav() {
    document.getElementById("mySidenav").style.width = "0";
    document.getElementById("main").style.marginLeft= "20%";
}
</script>


<div id="mySidenav" class="sidenav">
  <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a>
<a href="{% url 'home' %}" class="active">Home</a>

<a href = "{% url 'lessons:index' %}"class="active">Lessons</a>

<a href="{% url 'resources' %}"class="active">Resources</a>

</div>

<div id="main">
  <span style="font-size:30px;cursor:crosshair;display:inline-block" onclick="openNav()">&#9776;</span>

<h1>Exercise</h1>
<form id = "exercise" action= " ">
{% for question in lesson.question_set.all %}
{% autoescape off %}{{ question.question_text }} {% endautoescape %}<BR>
{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
{% for choice in question.choice_set.all %}
  {% if choice.answer %}
<input type="radio" name="question{{question.pk}}" value=1 />{% autoescape off %}{{ choice.choice_text }}{% endautoescape %}<BR>
  {% else %}
<input type="radio" name="question{{question.pk}}" value=0 />{% autoescape off %}{{ choice.choice_text }}{% endautoescape %}<BR>
  {% endif %}
<!--<label for="{{question.pk}}choice{{ forloop.counter }}"></label><br />-->

{% endfor %}
<BR>
{% endfor %}<BR><BR>
<input type="submit" value="Submit"><BR><BR>
</form>
</div>

<center><p id="grade">Your grade is: </p></center>

<button class="accordion"></button>
<div class="panel">
  {% for question in lesson.question_set.all %}
  {% autoescape off %}{{question.answer}} {% endautoescape %}
  {% endfor %}
<BR><BR>
  <p align=center><a href="/../lessons/{{lesson.lesson_id}}" class="backbutton"><span><font-size=14px>Return to Lesson</font></span></a>
    <a href="/../lessons/quiz/{{lesson.lesson_id}}" class="button"><span><font-size=14px>Quiz {{lesson.lesson_id}}</font></span></a><BR><BR>
</div>

<BR><BR><BR><BR><BR><BR><BR><BR><BR><BR><BR>

Javascript函数

  <script type='text/javascript'>

//start function
    <script>
  document.getElementById("exercise").onsubmit = function() {
  var result=0;
  { % for question in lesson.question_set.all % }
  result += parseInt(document.querySelector('input[name = "question{{question.pk}}"]:checked').value);
  { % endfor % }
  document.getElementById("grade").innerHTML = result;
  return false; // required to not refresh the page; just leave this here
  } //this ends the submit function
</script>
//end function






  var acc = document.getElementsByClassName("accordion");
  var i;

  for (i = 0; i < acc.length; i++) {
    acc[i].onclick = function() {
      this.classList.toggle("active");
      var panel = this.nextElementSibling;
      if (panel.style.maxHeight){
        panel.style.maxHeight = null;
      } else {
        panel.style.maxHeight = panel.scrollHeight + "px";
      }
    }
  }
  </script>

所以我使用Django创建一个网站,我无法弄清楚为什么“document.getElementById(”grade“)。innerHTML = result;”不改变“你的成绩是:

”我有在整个页面上移动了p标签,仍然无法通过innerHTML来更改标签中的内容。我还将结果更改为不同的值,但仍然不会更改p标记。我还将所有javascript函数放在同一个脚本中。我希望页面提交测验并显示结果。我究竟做错了什么?后来,我想说明哪些问题不正确以及哪些是正确的答案。 我很感激你的帮助。谢谢!

以下是更正的脚本:

<script>
document.getElementById("submit").onclick = function() {
var result=0;
var count=0;
{% for question in lesson.question_set.all %}
result += parseInt(document.querySelector('input[name = "question{{question.pk}}"]:checked').value);
count++;
{% endfor %}
document.getElementById("grade").innerHTML = "You got a "+result+"/"+count+". If you would like to retake the quiz please resubmit!";
return false; // required to not refresh the page; just leave this here
} //this ends the submit function
</script>

1 个答案:

答案 0 :(得分:5)

getElementById而非getElementsById

document.getElementById("grade").innerHTML = result;