简体   繁体   English

将元素附加到 DOM 后如何重新加载脚本

[英]How to reload script after append element to DOM

Hello I am working with LIKEBTN I have some designs on which I need likes and I have added the likebtn library on the designs but there is a load more which get the old designs as well and after ajax the html appends on the page and there I am not able to load like button library.您好,我正在与LIKEBTN合作,我有一些我需要喜欢的设计,并且我在设计上添加了 likebtn 库,但是还有更多负载可以获取旧设计,并且在 ajax 之后,html 附加到页面上,然后我无法像按钮库一样加载。 As script has loaded before on page load so it's not appending on the new DOM elements.由于脚本在页面加载之前已加载,因此它不会附加到新的 DOM 元素上。 I have tried .我试过了 。 live and .delegate as well but couldn't find the solution. live.delegate也是如此,但找不到解决方案。

在加载更多之前 You can see there is a heart icon which has the like functionality and in console you will find child elements inside <span> as well which loaded by likebtn library but after I load more designs by ajax I couldn't load likebtn library scripts on that elements you can see the image there is no like icon and in console there is no child elements inside <span> .您可以看到有一个具有类似功能的心形图标,并且在控制台中,您还会在<span>找到子元素,这些子元素由likebtn库加载,但是在我通过 ajax 加载更多设计后,我无法加载likebtn库脚本您可以看到图像中的元素没有类似图标,并且在控制台中<span>没有子元素。

加载更多后

This is my html which appends on ajax load more.这是我的html ,它附加在 ajax 加载更多。 it has dynamic variable coming from database.它有来自数据库的动态变量。

                <a data-index="{{ loop.index0 }}" class="galimg animate" data-animation="fadeInUp" data-duration="010">
                 <div class="gimg">
                    <img class="img-fluid" src="{% path (design.designThumbnail), 'thumbnail' %}" alt="" />
                    <div class="gimg-caption">
                        <div class="title">
                            <h6>{{ design.title }}</h6>
                            <p>By: {{ (design.userId.userFullname!= ' ')? design.userId.userFullname|uppercase_first_letter : design.userId.contactName|uppercase_first_letter }}</p>
                            <p>From: United Kingdom</p>
                        </div>
                        <div class="likes">=
                            <!-- LikeBtn.com BEGIN -->
                            <span class="likebtn-wrapper" data-theme="custom" data-icon_size="24" data-icon_l="hrt6" data-icon_d="sml3-u" data-icon_l_c="#ffffff" data-icon_l_c_v="#fb0505" data-label_c="#ffffff" data-label_c_v="#ffffff" data-counter_l_c="#ffffff" data-bg_c="rgba(250,250,250,0)" data-bg_c_v="rgba(250,250,250,0)" data-brdr_c="rgba(198,198,198,0)" data-i18n_like="Love this" data-white_label="true" data-identifier="btn_design_{{ loop.index }}" data-dislike_enabled="false" data-icon_dislike_show="false" data-counter_zero_show="true" data-popup_disabled="true" data-share_enabled="false" data-tooltip_enabled="false" data-site_id="5b7ac72c6fd08b366d431a36"></span>
                            <!-- LikeBtn.com END -->
                        </div>
                    </div>
                 </div>
            </a>

This is the library I need to load after ajax succes这是我在 ajax 成功后需要加载的库

<script>(function(d,e,s) {if(d.getElementById("likebtn_wjs"))return;a=d.createElement(e);m=d.getElementsByTagName(e)[0];a.async=1;a.id="likebtn_wjs";a.src=s;m.parentNode.insertBefore(a, m)})(document,"script","//w.likebtn.com/js/w/widget.js");</script>

You can use init() method after ajax success.您可以在 ajax 成功后使用 init() 方法。 It will render your new elements.它将呈现您的新元素。

if (typeof(LikeBtn) != "undefined") {
    LikeBtn.init();
}

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

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