javascript - 关于 javascript onclick foreach 函数

标签 javascript

我想为特定帖子 ID 的 foreach 帖子显示表单 onclick 函数。我正在尝试这样做,但它不起作用。

 {% for post in posts %}

            <div post-id="{{ post.id }}" class="post-box
            w3-container w3-card-2 w3-white w3-round w3-margin"><br>
            {# <img src="w3images/avatar6.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px"> #}
            <span class="w3-right w3-opacity">{{ post.created }}</span>
            <h4>{{ user.name }}</h4><br>
            <hr class="w3-clear">
            <p><a href="/posts/{{ post.id }}">{{ post.title }}</a></p>
            {# <img src="w3images/nature.jpg" style="width:100%" class="w3-margin-bottom"> #}
            <p>{{ post.description }}</p>
            <button type="button" class="w3-button w3-theme-d1 w3-margin-bottom"><i class="fa fa-thumbs-up"></i>  Like</button>
            <button class="w3-button w3-theme-d2 w3-margin-bottom" id="comment"><i class="fa fa-comment"></i>Comment</button>
            <div id="comment-form-{{ post.id }}" style="display: none;">
                <textarea></textarea>
                <button type="button"></button>
            </div>
        </div>

       {% endfor %}


<script>
      var postBoxes = document.getElementsByClassName('post-box')
       postBoxes.forEach(function (postBox) {
      var postId = postBox.getAttribute('post-id')
      postBox.onclick = function () {
        document.getElementById('comment-form-' + postId).style.display = 'block'
    }
})
 </script>

最佳答案

像这样更改代码,在单击事件中声明 post-id。并使用 querySelectorAll() 更改选择器.并且还可以与 addEventListener() 一起使用而不是正常的点击

工作示例

var postBoxes = document.querySelectorAll('.post-box')
postBoxes.forEach(function(postBox) {

  postBox.addEventListener('click', function() {
    var postId = this.getAttribute('post-id')
  console.log(postId)
    document.getElementById('comment-form-' + postId).style.display = 'block'
  })
})
<div post-id="{{ post.id }}" class="post-box
            w3-container w3-card-2 w3-white w3-round w3-margin"><br> {# <img src="w3images/avatar6.png" alt="Avatar" class="w3-left w3-circle w3-margin-right" style="width:60px"> #}
  <span class="w3-right w3-opacity">{{ post.created }}</span>
  <h4>{{ user.name }}</h4><br>
  <hr class="w3-clear">
  <p><a href="/posts/{{ post.id }}">{{ post.title }}</a></p>
  {# <img src="w3images/nature.jpg" style="width:100%" class="w3-margin-bottom"> #}
  <p>{{ post.description }}</p>
  <button type="button" class="w3-button w3-theme-d1 w3-margin-bottom"><i class="fa fa-thumbs-up"></i>  Like</button>
  <button class="w3-button w3-theme-d2 w3-margin-bottom" id="comment"><i class="fa fa-comment"></i>Comment</button>
  <div id="comment-form-{{ post.id }}" style="display: none;">
    <textarea></textarea>
    <button type="button"></button>
  </div>
</div>

关于javascript - 关于 javascript onclick foreach 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44194479/

相关文章:

javascript - $q.all() 中的错误处理

javascript - Raphael js-onclick 改变颜色的 Action ?保持那个颜色直到返回点击其他

javascript - 如果我的 JavaScript 函数返回 false,我该如何阻止表单操作?

javascript - 使用 Layout, Partials with Handlebars 模板

javascript - browserify 两个单独的包传递第二个文件中的变量

javascript - LI 和 child 的 JQuery 鼠标悬停

javascript - 不在每个页面上包含 JS 和 CSS 文件有什么好处吗?

javascript - 使用 Array.forEach 迭代 getElementsByClassName 的结果

javascript - 如何在悬停时转换图像?

javascript - wordpress 的本地主机 : correct way to link reference css/js files?