简体   繁体   English

使用 jquery 迭代动态内容

[英]iterating over dynamic content with jquery

var deneme = document.getElementsByClassName("content-holder");
var uzat = document.getElementsByClassName("uzat");
for (var i = 0; i < deneme.length; i++) {
    var yuksek = deneme[i].offsetHeight;
    if (yuksek > 250) {
        deneme[i].style.height = "97px";
        uzat[i].style.display = "block"
    }
};

This function finds entries in a page that are longer than 250 px and shrinks them to 97 px, also adds a button "uzat" and when this button is clicked entry gets back to the original size.此 function 在页面中查找长度超过 250 像素的条目并将它们缩小到 97 像素,还添加了一个按钮“uzat”,当单击此按钮时,条目将恢复为原始大小。

Now I'm using infinite scroll to load new entries and this function doesn't work on newly loaded entries.现在我正在使用无限滚动来加载新条目,而这个 function 不适用于新加载的条目。 I tried to trigger the function every time new content is added but it also affected the old entries.每次添加新内容时,我都尝试触发 function,但它也影响了旧条目。 Is there any way to change only the new entries without affecting the old ones?有没有办法只更改新条目而不影响旧条目?

Infinite scroll html:无限滚动html:

<div class="infinite-container">
    {% for entry in entries_list %}
    <div class="entry-yazar infinite-item">
        <span class=title-span>{{entry.title}}</span>
        <p class=content-holder id={{entry.id}}>
            {% if entry.safe %}{{entry.content|safe|linebreaksbr}}{% else %}{{entry.content|linebreaksbr}}{% endif %}
        </p>
        <button id="{{entry.id}}b" onclick="larger('{{entry.id}}','{{entry.id}}b')" class=uzat>doyamadım...</button>
        <form method="GET">
            {% csrf_token %}

            {% if own_entry > 0 %}
            <div class=entry-bottom>
                <span class=num-holder>{{entry.own_like}}</span>
                <img class=icon3 src="{% static 'icons\facebook.png' %}" alt="facebook">
                <img class=icon4 src="{% static 'icons\twitter.png' %}" alt="twitter">
                <time>
                    {% if best_one == entry.id %}
                    <img class=star-icon src="{% static 'icons\golden-star.png' %}" alt="like">
                    {% endif %}
                    {% if today_best_one == entry.id %}
                    <img class=star-icon src="{% static 'icons\bronze-star.png' %}" alt="like">
                    {% endif %}
                    <span class=timer>{{entry.created_date|date:'Y-m-d H:i'}}</span>
                </time>
                <div class=user-holder>
                    <a href="/entries/delete/{{entry.id}}"><img class=edit-icon src="{% static 'icons\delete.png' %}"
                            alt="sil"></a>
                    <a href="/entries/edit/{{entry.id}}"><img class=edit-icon src="{% static 'icons\edit2.png' %}"
                            alt="edit"></a>
                </div>

                {% else %}

                <form method="GET">
                    {% csrf_token %}

                    {% if entry.already_liked %}
                    <button type="button" onclick="like('{{entry.id}}')" class=active-button id=like-button{{entry.id}}><img
                            class=icon5 src="{% static 'icons\virus.png' %}" alt="like"></button>
                    <button style="display:none" onclick="like('{{entry.id}}')" class=active-button type="button"
                        id=dislike-button{{entry.id}}><img class=icon5 src="{% static 'icons\passive-virus.png' %}"
                            alt="dislike"></button>
                    <img class=icon3 src="{% static 'icons\facebook.png' %}" alt="facebook">
                    <img class=icon4 src="{% static 'icons\twitter.png' %}" alt="twitter">
                    <time>
                        {% if best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\golden-star.png' %}" alt="like">
                        {% endif %}
                        {% if today_best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\bronze-star.png' %}" alt="a-like">
                        {% endif %}
                        {% if followed_best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\purple-star.png' %}" alt="f-like">
                        {% endif %}
                        {% if today_followed_best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\blue-star.png' %}" alt="af-like">
                        {% endif %}
                        <span class=timer>{{entry.created_date|date:'Y-m-d H:i'}}</span>
                    </time>
                    {% else %}
                    <button style="display:none" onclick="like('{{entry.id}}')" class=active-button type="button"
                        id=like-button{{entry.id}}><img class=icon5 src="{% static 'icons\virus.png' %}" alt="like"></button>
                    <button type="button" id=dislike-button{{entry.id}} class=active-button onclick="like('{{entry.id}}')"><img
                            class=icon5 src="{% static 'icons\passive-virus.png' %}" alt="dislike"></button>
                    <img class=icon3 src="{% static 'icons\facebook.png' %}" alt="facebook">
                    <img class=icon4 src="{% static 'icons\twitter.png' %}" alt="twitter">
                    <time>
                        {% if best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\golden-star.png' %}" alt="like">
                        {% endif %}
                        {% if today_best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\bronze-star.png' %}" alt="a-like">
                        {% endif %}
                        {% if followed_best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\purple-star.png' %}" alt="f-like">
                        {% endif %}
                        {% if today_followed_best_one == entry.id %}
                            <img class=star-icon src="{% static 'icons\blue-star.png' %}" alt="af-like">
                        {% endif %}
                        <span class=timer>{{entry.created_date|date:'Y-m-d H:i'}}</span>
                    </time>
                    {% endif %}

    </div>
    {% endif %}
    {% endfor %}

Infinite scroll jquery:无限滚动jquery:

 var infinite = new Waypoint.Infinite({
        element: $('.infinite-container')[0],

      });

Easiest way would be to add new entries with some type of DOM attribute that you will immediatelly remove after processing or the other way around just add one that says it was processed最简单的方法是添加具有某种类型的 DOM 属性的新条目,您将在处理后立即删除,或者相反,只需添加一个表示已处理的条目

like ie <element data-processed="true" />比如<element data-processed="true" />

or use additional class that you will remove on processing and search for it.或使用额外的 class ,您将在处理和搜索时删除它。

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

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