简体   繁体   English

实际上我想运行每个循环和 append 它,但是在 append 里面我想使用模板文字运行另一个循环但是它抛出错误

[英]Actually I want to run each loop and append it, but inside append I want to run another loop using template literals but it throw error

please concern on my code, and find solutions for me, Actually I want to run each loop and append it, but inside append I want to run another loop using template literals but it throw error.请关注我的代码,并为我找到解决方案,实际上我想运行每个循环和 append 它,但在 append 中我想使用模板文字运行另一个循环但它抛出错误。 Uncaught SyntaxError: Missing } in template expression未捕获的语法错误:模板表达式中缺少 }

$(document).ready(function(){ comments =; $(文档).ready(函数(){ 评论=;

        $.each(comments,function(key,value){
            
            $('#commentContainer').append(`
            
            <div class="commentContainer mt3">
                <div class="commentbox">
                    <div class="commentPerson d-flex justify-content-between">
                        <div class="commentsubPerson d-flex">
                            <div class="commentedPersonimg">
                                <img src="https://thumbs.dreamstime.com/b/default-avatar-profile-image-vector-social-media-user-icon-potrait-182347582.jpg" alt="">
                            </div>
                            <div class="comentedPersonName mt0">
                                <label for="">Test</label>
                            </div>
                        </div>
                        <div class="commentDate">
                            <label for="">4 days ago</label>
                        </div>
                        </div>
                        <div class="comment">
                            <p>New Comment From User</p>
                        </div>
                        
                        <div>
                            <button type="button" class="replyButton rep" data-id="" id="replybutton">Reply</button>
                            <button class="replyCounter"><span>1</span> Reply</button>
                        </div>
                        
                        <form class="comment_reply " id="reply_request_form" data-id="">
                            <input type="hidden" name="_token" value="thrWB62yPQnmIQrhGX918omsXgQIxg0nOnN4GdFQ">                                            <div class="inputBox">
                            <input type="hidden" class="hidden" id="comment_id" value="102">
                            <input type="hidden" class="hidden" id="user_id" value="">
                            <input type="hidden" class="hidden" id="theme_id" value="29">
                            <input type="text" class="hidden" id="reply_text" placeholder="Write a Reply" value="" required="">
                            <button type="button" class="replyButton mt-2 reply_request">Send</button>
                        </form>
                    </div>
                    
                    
                        
                        
                      ${  
                            $.each(value.replycomments,function(ind,val){ `
                                
                                <p>here something goes related html</p>

                                `                            
                            });

                        }
                        
                        
                         

                </div>
            </div>
            
            
            
            `)
            
            
        });
        
    });
  • In the second loop you should return whatever is between template literals.在第二个循环中,您应该return模板文字之间的任何内容。

  • You have extra brackets in your code if that is all.如果仅此而已,您的代码中还有额外的括号。 Try formatting the code to see the problem using a code formatter.尝试使用代码格式化程序格式化代码以查看问题。 Prettier is one of the most common code formatters which is available both as a VS Code extension and online . Prettier是最常见的代码格式化程序之一,可作为 VS 代码扩展和在线使用。

This should work in this case:这应该适用于这种情况:

$.each(comments, function (key, value) {
  $("#commentContainer").append(`
    <div class="commentContainer mt3">
      <div class="commentbox">

        <div class="commentPerson d-flex justify-content-between">
          <div class="commentsubPerson d-flex">
            <div class="commentedPersonimg">
              <img
                src="https://thumbs.dreamstime.com/b/default-avatar-profile-image-vector-social-media-user-icon-potrait-182347582.jpg"
                alt="">
            </div>
            <div class="comentedPersonName mt0">
              <label for="">Test</label>
            </div>
          </div>
          <div class="commentDate">
            <label for="">4 days ago</label>
          </div>
        </div>

        <div class="comment">
          <p>New Comment From User</p>
        </div>

        <div>
          <button type="button" class="replyButton rep" data-id="" id="replybutton">Reply</button>
          <button class="replyCounter"><span>1</span> Reply</button>
        </div>

        <form class="comment_reply " id="reply_request_form" data-id="">
          <input type="hidden" name="_token" value="thrWB62yPQnmIQrhGX918omsXgQIxg0nOnN4GdFQ">
          <div class="inputBox">
            <input type="hidden" class="hidden" id="comment_id" value="102">
            <input type="hidden" class="hidden" id="user_id" value="">
            <input type="hidden" class="hidden" id="theme_id" value="29">
            <input type="text" class="hidden" id="reply_text" placeholder="Write a Reply" value="" required="">
            <button type="button" class="replyButton mt-2 reply_request">Send</button>
        </form>

        ${$.each(value.replycomments, function (ind, val) {
          return `<p>here something goes related html</p>`
        })}
      </div>
    </div>
  `)
})

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

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