当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Replace on load content

问题描述:

I want to change content unicode with icons on load, but it does not work because I'm loading content with jQuery after the page loads. I use this code:

This is "custom.js" file.

var $ = jQuery.noConflict();

$(function() {

var canRun = false;

$.ajax({

url: './includes/loadfeed.php',

cache: false,

beforeSend: function(){

$('#tiles').append('<li id="loadingimg"><img src="images/loading.gif" width="80" height="80" style="margin:auto;" /></li>');

},

success: function(html){

$('#tiles').append(html);

$('#loadingimg').remove();

canRun = true;

}

});

$(window).scroll(function() {

if( ($(window).scrollTop() + $(window).height() > $(document).height() - 10 ) && (canRun == true ) ) {

canRun = false;

var newCount = $('#next_id').data('newcount');

var maxid = $('#next_id').data('maxid');

$.ajax({

type: 'GET',

url: '/includes/loadfeed.php?next',

data: { newcount: newCount, max_id: maxid },

dataType: "text",

cache: false,

beforeSend: function(){

$('#tiles').append('<li id="loadingimg"><img src="images/loading.gif" width="80" height="80" style="margin:auto;" /></li>');

$('#next_id').remove();

},

success: function(xxx){

$('#loadingimg').remove();

$('#tiles').append(xxx);

canRun = true;

}

});

}

}); });

And this is replace code. "getomeji" have in loaded content.

$(document).on('load','#getemoji',function(){

var el = $(this).get(0);

el.innerHTML = el.innerHTML.replace(

new RegExp(ranges.join('|'), 'g'),

'<span class="emoji" data-emoji="$&"></span>'

);

});

This code is working with "click" events but not working with "on load". Can someone help me?

网友答案:

How about doing it as document is ready ...

$(document).ready(function() {
  $("#getemoji").replace(
    new RegExp(ranges.join('|'), 'g'), 
    '<span class="emoji" data-emoji="$&"></span>'
  );  
});
网友答案:
$(document).ready(function(){
    $.ajax({
         url: './includes/loadfeed.php',
         cache: false,
         beforeSend: function(){
                    $('#tiles').append('<li id="loadingimg"><img src="images/loading.gif" width="80" height="80" style="margin:auto;" /></li>');
                      },
          success: function(html){
                    $('#tiles').append(html);
                    $('#loadingimg').remove();
                            canRun = true;
                    $('#getemoji').load(function(){
                       var el = $(this).get(0);
                       el.innerHTML = el.innerHTML.replace(
                       new RegExp(ranges.join('|'), 'g'), 
                       '<span class="emoji" data-emoji="$&"></span>'
                        );  
                    });

                    }

     });

  });
网友答案:

i found answer.

$("#tiles").contents().each(function () {
                                if (this.nodeType === 3) this.nodeValue = $.trim($(this).text()).replace(new RegExp(ranges.join('|'), 'g'),'<span class="emoji" data-emoji="$&"></span>')
                                if (this.nodeType === 1) $(this).html( $(this).html().replace(new RegExp(ranges.join('|'), 'g'),'<span class="emoji" data-emoji="$&"></span>'))
});

Best regards for your comments.

分享给朋友:
您可能感兴趣的文章:
随机阅读: