简体   繁体   English

无法使用 javascript 在 for 循环中切换文本

[英]Trouble toggling text in for loop, using javascript

I have a list of cards displayed via a for loop.我有一个通过 for 循环显示的卡片列表。 On click I want to toggle some text.单击时我想切换一些文本。 It works fine for the top card, but when I click the card below, it toggles the first card.它适用于最上面的卡片,但是当我单击下面的卡片时,它会切换第一张卡片。 This is the case even though i've given the cards different id's used in the javascript toggle function.即使我在 javascript 切换功能中为卡片提供了不同的 ID,情况也是如此。

Any ideas about what i'm doing wrong?关于我做错了什么的任何想法?

<!-- HTML -->
        {% for card in cards %}
                  <div class="card mb-3">
                    <div class="card-header">
                      <img class = "float:left mr-2 mt-1 small-profile-picture" src ="{{ card.creator.profile.image.url }}" >
                      <a class="mb-4 float-up"  > {{ card.creator }}</a>
                       <small> <p class=mt-2> {{ card.date }}  </p> </small>
        
                    </div> <!-- Card Header ender  -->
        
                    <!-- Here we have the questions and answers -->
                    <div onclick="myFunction()" style="cursor: pointer;">
                      <div class="card-body mt-4">
                        <h5 class="card-title"> <a class="article-title">{{ card.question }}</a></h5>
                        <p id="myDIV{{card.card_id}}"> Click to see Answer </p>
                        <!-- <p class="card-text"> {{ card.answer}} </p> -->
                      </div> <!-- Card body ender -->
                    </div>
    
<!--Javascript -->
              <script type="text/javascript">
                function myFunction() {
                    var x = document.getElementById("myDIV{{card.card_id}}");
                    if (x.innerHTML === "Click to see Answer") {
                      x.innerHTML = "{{card.answer}}";
                    } else {
                      x.innerHTML = "Click to see Answer";
                    }
                  }
    
              </script>
{% endfor %}

Thanks for reading谢谢阅读

There's a lot of things I would do differently here if I'm honest, but to keep the solution as close to your code as possible, I'd suggest the following.老实说,有很多事情我会在这里做不同的事情,但为了使解决方案尽可能接近您的代码,我建议如下。 Remove the <script> from within your loop - you're redefining the function every time it loops.从循环中删除<script> - 每次循环时都要重新定义函数。 This is unnecessary and will probably cause a bunch of undesired effects.这是不必要的,可能会导致一系列不良影响。

Separate the JavaScript and pass myFunction arguments, rather than hard-coding strings into the function.分离 JavaScript 并传递myFunction参数,而不是将字符串硬编码到函数中。

This is untested, but should point you in the right direction.这是未经测试的,但应该为您指明正确的方向。

<script type="text/javascript">
    function myFunction(id, answer) {
         // note the template literal syntax below,
         // this is different to the handlebars provided by django.
        let x = document.getElementById(`card-${id}`);
        if (x.innerHTML === "Click to see Answer") {
            x.innerHTML = answer;
        } else {
            x.innerHTML = "Click to see Answer";
        }
    }
</script>

{% for card in cards %}
<div class="card mb-3">
    <div class="card-header">
        <img class = "float:left mr-2 mt-1 small-profile-picture" src="{{ card.creator.profile.image.url }}" >
        <a class="mb-4 float-up"> {{ card.creator }}</a>
        <small> <p class=mt-2> {{ card.date }}  </p> </small>
    </div>

    <div onclick="myFunction({{ card.card_id }}, {{ card.answer }})" style="cursor: pointer;">
        <div class="card-body mt-4">
        <h5 class="card-title"> <a class="article-title">{{ card.question }}</a></h5>
        <p id="card-{{ card.card_id }}"> Click to see Answer </p>
        </div>
    </div>
</div>
{% endfor %}

I changed a bit the questions and answers block:我改变了一些问题和答案块:

 function myFunction(ele) { ele.querySelector('.card-body.mt-4 .card-text').classList.toggle('d-none'); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"> <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js"></script> <div onclick="myFunction(this)" style="cursor: pointer;"> <div class="card-body mt-4"> <h5 class="card-title"> <a class="article-title">{{ card.question }}</a></h5> <p id="useless...."> Click to see Answer </p> <!--- removed comment from the next line --> <p class="card-text d-none"> This is the answer ....{{ card.answer}} </p> </div> <!-- Card body ender --> </div>

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

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