简体   繁体   English

如何在没有这个错误的情况下在 Jquery 中使用“for 循环”?

[英]How can I use "for loops" in Jquery without this mistake?

Here's the problem: I have a simple unordered list of posts.问题是:我有一个简单的无序列表的帖子。 Each post is truncated (250 chars), but should expand when clicked (Using .replaceWith).每个帖子都被截断(250 个字符),但应在单击时展开(使用 .replaceWith)。 What I'm trying to do is replace the truncated Post.content with the "untruncated" Post.content.我想要做的是用“未截断的”Post.content 替换截断的 Post.content。 When I try this, the click returns the content of all the Posts in my list.当我尝试此操作时,单击会返回列表中所有帖子的内容。 I only want the content of the Post that I am clicking.我只想要我点击的帖子的内容。 Here's the relevant code:这是相关的代码:

<ul>

    {% for post in object_list %}<tr class="{% cycle 'row1' 'row2' %}"></tr>
        <div class="panel panel-default col-sm-offset-1 col-sm-8">
    <p><h4><B><a class="title" href="/posts/{{ post.id }}/">{{post.title }}</a></B></h4></p>  
                    <p style="color:gray">Author:
            <a style="color:darkgray" href="{% url 'accounts:profile' %}"> <span class="link">{{ user.first_name }} {{ user.last_name }}</span></a></p>
        <hr style="">
        <div class="post" >
            <h5 style="color: rgba(101, 106, 106, 1)">{{post.content|truncatechars:250 }}</h5>
        </div>
            <div class="expandContent">
            <button class="btn btn-primary btn-sm">Read</button>
            </div>
            <br>
        <br>
        <br>
        <div class="content">
            <p>{{ post.content }}</p>
        </div>
        </div>
        <br>

    {% endfor %}

</ul>   

Above is my basic list.以上是我的基本清单。 Here is the JS that should expand the Post.content:这是应该扩展 Post.content 的 JS:

<script src="{% static 'posts/jquery-3.1.1.js' %}"></script>
<script type="text/javascript">
$(".expandContent").click(function(){$(".post").replaceWith($('.content'));
});

</script>

If any more information or code would be helpful, please let me know.如果有更多信息或代码会有所帮助,请告诉我。 If it's at all relevant, this project is on Django 1.10.如果它完全相关,则该项目位于 Django 1.10 上。 Thank you!谢谢!

You can do something like this你可以做这样的事情

<ul>

    {% for post in object_list %}<tr class="{% cycle 'row1' 'row2' %}"></tr>
        <div class="panel panel-default col-sm-offset-1 col-sm-8">
    <p><h4><B><a class="title" href="/posts/{{ post.id }}/">{{post.title }}</a></B></h4></p>  
                    <p style="color:gray">Author:
            <a style="color:darkgray" href="{% url 'accounts:profile' %}"> <span class="link">{{ user.first_name }} {{ user.last_name }}</span></a></p>
        <hr style="">
        <div class="post" >
            <h5 style="color: rgba(101, 106, 106, 1)">{{post.content|truncatechars:250 }}</h5>
        </div>
            <div class="expandContent">
            <button class="btn btn-primary btn-sm" onclick="replace("content-1">Read</button>
            </div>
            <br>
         <br>
        <br>
        <div class="content" id="content-1">
            <p>{{ post.content }}</p>
        </div>
        </div>
        <br>

    {% endfor %}

</ul> 

and in the replace function you can getelementbyid and display it.在替换功能中,您可以获取elementbyid 并显示它。

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

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