简体   繁体   English

Ajax内容更改时如何调用函数?

[英]How to call function when ajax content changes?

So i'm working on a shopify site and stuck on a script m writing, the situation is that there is a plugin installed to insert reviews on the site, and the task is to make every user have a "verified buyer" tag just after the user name even if its not a verified user, since the real verified user have a tag through the plugin, the job is to insert it after every user expect those which already have it, I was able to do it using this script - 因此,我在shopify网站上工作,并停留在编写脚本的情况下,情况是安装了一个插件以在网站上插入评论,而任务是使每个用户在紧随其后都具有“ verified Buyer”标签用户名,即使它不是经过验证的用户,因为真正的经过验证的用户通过插件具有标签,所以工作是在每个用户期望已经拥有它的用户之后插入它,我能够使用此脚本来执行此操作-

$(document).ajaxStart(function(){

function addVerified(){

    var list=document.querySelectorAll('display-wrapper .nav-content .reviews-active .review');
    for(let li of list){

        if(!li.classList.contains('hidden')&& (!li.firstElementChild.classList.contains('verified-buyer'))){
            let needElem=li.firstElementChild.children[1].firstElementChild;
            needElem.insertAdjacentHTML('afterend','<div class="label-with-tooltip pull-left">  <span class="y-label>Verified Buyer</span>   </div>'); 

        }
    }
}

addVerified();

});

Everything works right expect when pagination below the reviews is used, the content changes and the reviews changes and the function does not work thus making the verified buyer tag removed, I have tried DOMSubtreeModified (made the script stuck in infinite loop), ajaxComplete to execute the function, but nothing have worked so far, I have not much knowledge of javascript I'm still learning, after getting too frustrated I decided to post it here. 当使用评论下面的分页,内容更改和评论更改且该功能无法正常工作时,一切工作均正常,因此使已验证的买方标签被删除,我尝试使用DOMSubtreeModified(使脚本陷入无限循环),ajaxComplete执行函数,但到目前为止没有任何效果,我对仍在学习的javascript知识不多,在感到沮丧之后,我决定将其发布在这里。 Please help 请帮忙

Okay... With the rest of your markup I can suggest something. 好吧...在您标记的其余部分中,我可以提出一些建议。

That is a lie made to customers... I don't like that. 这是对客户的谎言……我不喜欢那样。

But anyway, here is the way to tweak on it: 但是无论如何,这是对其进行调整的方法:

// Create the element to insert
var fake_verified_buyer = $('<div class="label-with-tooltip pull-left">  <span class="y-label yotpo-user-title yotpo-action-hover" data-type="toggleFade" data-target="yotpo-tool-tip">Verified Buyer</span>  <div class="yotpo-tool-tip yotpo-animation-fade" data-user-type="yotpo-verified-buyer"> <div class="tool-tip-header"> <span>What is a</span> <span class="header-green">Verified Buyer</span> </div> <div class="tool-tip-content"> A Verified Buyer is a user who has purchased the reviewed product through our store. </div> </div> </div>');

// On pagination click, insert that element after each user name, if not already present.
$(document).on("click",".yotpo-pager",function(){
  setTimeout(function(){
    $(document).find(".yotpo-user-name").each(function(){
      if(!$(this).next().is(".label-with-tooltip")){

        var clone = fake_verified_buyer.clone();
        clone.insertAfter(this);
      }
    });
  },100);
});

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

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