简体   繁体   English

如何在django模板中用JS更改添加和删除标签

[英]How to change add and remove tags with JS in django template

I have a quiz Django app which consists of two parts.我有一个测验 Django 应用程序,它由两部分组成。 One is showing 10 sentences with their audio to remember, one per page, and the second is asking questions for the same set of sentences.一个是展示 10 个句子及其音频以供记忆,每页一个,第二个是针对同一组句子提问。 First part was set up with js function which creates pagination in my html the following way:第一部分是使用 js function 设置的,它通过以下方式在我的 html 中创建分页:

my_template.html my_template.html

<button id="prev">prev</button>
<button id="next">next</button>
<ul class="list-articles" id="dd">

</ul>

<script>
var my_objects =  `{{ my_objects|safe}}:` #list of items from my view function

function paginate(action) {
        console.log(action)
        if (action ==='next') {
            page_number++;
        }
        else{
                page_number--;
            }

       const audio = document.createElement('audio');
       audio.src =`/media/${my_objects[page_number].fields.audio}`;

        $('#dd').empty();
        $('#dd').append('<li><h1>'+ my_objects[page_number].fields['content'] +'</h1></li>');
        $('#dd').append(audio); 
        $('#page_number').val(page_number);
    } 

  document.addEventListener('DOMContentLoaded', function() {
      document.querySelector('#next').onclick = function() {
           paginate('next'); #same goes for 'prev' button.
}
  })
</script>

Now when the user paginated through all the sentences I want to show the continue button and if the user clicks it, start the second part.现在,当用户对所有句子进行分页时,我想显示continue按钮,如果用户单击它,则开始第二部分。 The second part has absolutely same page except I need to hide content of my objects and leave only audio or vice versa and add textarea tag for the user's input.第二部分具有完全相同的页面,只是我需要隐藏对象的内容并仅保留音频,反之亦然,并为用户输入添加 textarea 标签。 After that I need to loop over my objects again - now in the form of questions.之后我需要再次遍历我的对象——现在以问题的形式。 I need to do that without page re-rendering so I don't need to store the list of objects or query the DB again.我需要在不重新呈现页面的情况下执行此操作,因此我不需要存储对象列表或再次查询数据库。

I tried to make it with tag disabling and activating the second loop after the first ends but that looks quite messy and I suppose there is a smarter way of doing that.我试图在第一个循环结束后通过标记禁用和激活第二个循环来实现它,但这看起来很混乱,我想有一种更聪明的方法可以做到这一点。 I'm not a JS developer so any help would be appreciated!我不是 JS 开发人员,所以任何帮助将不胜感激!

Here is one way you can approach this problem:这是解决此问题的一种方法:

In your HTML template, add a "Continue" button that is initially hidden.在您的 HTML 模板中,添加一个最初隐藏的“继续”按钮。 When the user reaches the last page of the first part, show the "Continue" button using JavaScript. You can do this by adding a display: block style to the button element or by adding a class to the button element that includes the display: block style.当用户到达第一部分的最后一页时,使用 JavaScript 显示“继续”按钮。您可以通过向按钮元素添加 display: block 样式或向包含显示的按钮元素添加 class 来实现此目的:块样式。 When the user clicks the "Continue" button, hide the sentences and audio elements and show the textarea elements using JavaScript. You can do this by adding a display: none style to the sentences and audio elements and a display: block style to the textarea elements, or by adding and removing classes to the elements that include the appropriate styles. Start the second loop over the objects, this time showing the textarea elements and hiding the sentences and audio elements.当用户单击“继续”按钮时,隐藏句子和音频元素并使用 JavaScript 显示文本区域元素。您可以通过向句子和音频元素添加显示:无样式和向文本区域添加显示:块样式来实现此目的元素,或者通过向包含适当 styles 的元素添加和删除类。在对象上开始第二个循环,这次显示文本区域元素并隐藏句子和音频元素。 Here is some sample code that demonstrates this approach:以下是演示此方法的一些示例代码:

<button id="continue-button" style="display: none">Continue</button>

<div id="sentences-container">
  {% for object in my_objects %}
    <div class="sentence">
      <h1>{{ object.fields.content }}</h1>
      <audio src="path/to/audio"></audio>
      <div class="translation">{{ object.fields.translation }}</div>
    </div>
  {% endfor %}
</div>

<div id="textarea-container" style="display: none">
  {% for object in my_objects %}
    <div class="question">
      <h1>{{ object.fields.content }}</h1>
      <audio src="path/to/audio"></audio>
      <div class="translation" style="display: none">{{ object.fields.translation }}</div>
      <textarea></textarea>
    </div>
  {% endfor %}
</div>

<script>
  // Show the "Continue" button when the user reaches the last page
  if (page_number === my_objects.length - 1) {
    document.querySelector('#continue-button').style.display = 'block';
  }

  // Handle the "Continue" button click
  document.querySelector('#continue-button').onclick = function() {
    // Hide the sentences and audio elements
    document.querySelector('#sentences-container').style.display = 'none';

    // Show the textarea elements
    document.querySelector('#textarea-container').style.display = 'block';
  }
</script>

Thanks for all who viewed and not commented.感谢所有看过但未发表评论的人。 I found the answer myself.我自己找到了答案。 Just need to add JS functions which will empty the main tag and refill with necessary field, If anyone needs details.只需要添加将清空主标签并重新填充必要字段的 JS 函数,如果有人需要详细信息。 feel free to contact me.随时联系我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM