无法对动态加载的元素应用jquery函数

时间:2016-07-10 21:51:18

标签: javascript jquery html emojione

我正在使用名为EmojiOne的表情符号库,我使用他们的一个示例中的以下代码将文本转换为emojis,当我加载页面时它工作正常但是当一个新元素动态出现时它没有'在我刷新页面之前对它们进行处理。如何让它在动态加载时工作?

$(document).ready(function() {
    $(".convert-emoji").each(function() {
        var original = $(this).html();
        var converted = emojione.toImage(original);
        $(this).html(converted);
    });
});

以下是示例的链接:http://git.emojione.com/demos/class-convert.html

2 个答案:

答案 0 :(得分:1)

将此代码解压缩到一个函数并在您想要的位置调用此函数...或者设置间隔...

function refreshEmojis() {
    $(".convert-emoji").each(function() {
        var original = $(this).html();
        var converted = emojione.toImage(original);
        $(this).html(converted);
    });
}

$(document).ready(refreshEmojis); //on page load

$("#someButtonId").on("click", function() { //click of some button
    //some action...
    refreshEmojis();
});

setInterval(refreshEmojis, 100); //each 100 milliseconds

答案 1 :(得分:1)

$(document).ready(function() {
    doThing();
});

function doThing(){
    $(".convert-emoji").each(function() {
            var original = $(this).html();
            var converted = emojione.toImage(original);
            $(this).html(converted);
    });
}

function yourAppendFunction(){
    //here are you appending new element and the fire the function

    doThing();

}