简体   繁体   English

使用jQuery修改文本以在DOM中动态添加元素

[英]Modify text with jQuery for dynamically added elements in DOM

I have a page http://www.projectdemocracy.in/ where some elements are dynamically added (example - CSS class ".fyre-comment-count" showing "X Comment"). 我有一个页面http://www.projectdemocracy.in/ ,其中动态添加了一些元素(示例-CSS类“ .fyre-comment-count”显示“ X注释”)。

I wanted to modify the text once page is loaded. 页面加载后,我想修改文本。

I was able to change the text using console of jQuery 我能够使用jQuery控制台更改文本

$(".fyre-comment-count").html($(".fyre-comment-count").text().split(" ")[0]+" Conversations");

When I type the same in my page, it doesn't work. 当我在页面中键入相同的内容时,它将不起作用。

I also tried $(document).ready(function(){}); 我也尝试了$(document).ready(function(){}); but no luck (same with document.ajaxSuccess). 但没有运气(与document.ajaxSuccess相同)。

Similarly, document.on would be working only with 'click' events but I want it to be done by default. 同样,document.on将仅与'click'事件一起使用,但我希望默认情况下完成此操作。

$(document).on('click', '.fyre-comment-article', function() {
     //Code
    });

Does it have any 'load' type event in document.on? 在document.on中是否有任何“加载”类型的事件?

What should I do to accomplish this? 我应该怎么做才能做到这一点?

Thanks, 谢谢,

I analyzed your web page and saw the following code: 我分析了您的网页并看到以下代码:

var articleId = fyre.conv.load.makeArticleId(null);
fyre.conv.load({}, [{
    el: 'livefyre-comments',
    network: "livefyre.com",
    siteId: "351251",
    articleId: articleId,
    signed: false,
    collectionMeta: {
        articleId: articleId,
        url:  "http://projectdemocracy.in",//fyre.conv.load.makeCollectionUrl(),
    }
}], function() {});

It seems like the 3rd argument of fyre.conv.load method is a callback function that will be executed when all elements are generated. 似乎fyre.conv.load方法的第三个参数是一个回调函数,将在生成所有元素时执行。

So put your code inside this function. 因此,将您的代码放入此函数中。 It will be like this: 它将是这样的:

...
}], function() {
    console.log('callback');
    $(".fyre-comment-count").html($(".fyre-comment-count").text().split(" ")[0]+" Conversations");
});

Hope this helps! 希望这可以帮助!

EDIT 编辑

If it still doesn't work it may be because livefyre runs this callback before DOM elements are being created. 如果仍然无法使用,可能是因为livefyre在创建DOM元素之前运行了此回调。 The workaround is to put $(".fyre-comment-count").html(...); 解决方法是放入$(".fyre-comment-count").html(...); inside setTimeout with minimal delay: setTimeout内部,延迟最小:

...
}], function() {
    setTimeout(function() {
        $(".fyre-comment-count").html($(".fyre-comment-count").text().split(" ")[0]+" Conversations");
    }, 10);    //minimal delay. wait till livefyre generates all needed elements
});

EDIT 2 编辑2

This is another variant how to make it work: 这是使其工作的另一种方式:

...
}], function(widget) {
  widget.on('commentCountUpdated', function() {
    setTimeout(function() {
      $(".fyre-comment-count").html($(".fyre-comment-count").text().split(" ")[0]+" Conversations");
    }, 10);
  });
});

This widget has a commentCountUpdated event, so you have to subscribe on it, and every time the comments number is changing, your callback will be executed 这个小部件具有commentCountUpdated事件,因此您必须对其进行订阅,并且每次注释号更改时,都会执行您的回调

By the way, commentCountUpdated callback recieves a single argument - the number of comments, so you can rewrite your code as follows: 顺便说一句, commentCountUpdated回调仅接收一个参数-注释数,因此您可以按以下方式重写代码:

}], function(widget) {
  widget.on('commentCountUpdated', function(commentsNum) {
    setTimeout(function() {
      $(".fyre-comment-count").html(commentsNum + " Conversations");
    }, 10);
  });
});

jQuery有一个on load事件,如果这是您要查找的内容:

$(window).load(function(){ ... });

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

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