开发者

How to write js function return innerHTML?

I am trying to convert emoticon codes into emoticon images. I want to write it within a function. Here is original code without function yet :

$.get("showMsgLive.php?username=" + str + "&servertime=" + servertime + "&usertime=" + usertime + "&lastmsgID=" + lastmsgid, function(newitems){
    newitems = newitems.replace(/\(smile1\)/g, '<img src="images/emoticons/emobasic/smile1.gif" class="emoticon"/>')
    .replace(/\(rabbit18\)/g, '<img src="images/emoticons/emospecial/rabbit18.gif" class="emoticon"/>')
    .replace(/\(rabbit19\)/g, '<img src="images/emoticons/emobasic/rabbit19.gif" class="emoticon"/>')
    .replace(/\(rabbit20\)/g, '<img src="images/emoticons/emoadvance/rabbit20.gif" class="emoticon"/>')
    .replace(/\(sheep4\)/g, '<img src="images/emoticons/emospecial/sheep4.gif" class="emoticon"/>'); //end of special emo
    newitems.innerHTML = newitems;
    api.getContentPane().append(newitems);
}); 

Here is the code that I try to write it in function :

function convertTextEmo(newitems){
    newitems = newitems.replace(/\(smile1\)/g, '<img src="images/emoticons/emobasic/smile1.gif" class="emoticon"/>')
    .replace(/\(rabbit19\)/g, '<img src="images/emoticons/emospecial/rabbi开发者_开发问答t19.gif" class="emoticon"/>')
    .replace(/\(rabbit20\)/g, '<img src="images/emoticons/emobasic/rabbit20.gif" class="emoticon"/>')
    .replace(/\(sheep4\)/g, '<img src="images/emoticons/emoadvance/sheep4.gif" class="emoticon"/>'); //end of special emo
    return newitems;    
}

jQuery.get("showMsgLive.php?username=" + str + "&servertime=" + servertime + "&usertime=" + usertime + "&lastmsgID=" + lastmsgid, function(newitems){
    convertTextEmo(newitems);
    api.getContentPane().append(newitems);  
}); 

I didn't put innerHTML in the function because I don't know where and how to put it, and the output didn't success convert the code into image. May I know where or how to put innerHTML in the function and return the value?


newitems.innerHTML = newitems; should not even work as newitems is a string and not an HTML element.

I think all you have to do is:

api.getContentPane().append(convertTextEmo(newitems));

Another tip to improve your code: As the image names of are the same as identifiers in the text, you could just create an array of names:

var emoticons = ['smile1', 'rabbit18',...];

and loop over it:

function convertTextEmo(newitems, icons){
    var pattern, content;
    for(var i = icons.length; i--; ) {
        pattern = new RegExp('\(' + icons[i] + '\)', 'g');
        content = '<img src="images/emoticons/emospecial/' + icons[i] + '.gif" class="emoticon"/>';
        newitems = newitems.replace(pattern, content);
    }
    return newitems;
}


jQuery.get("showMsgLive.php?username=" + str + "&servertime=" + servertime + "&usertime=" + usertime + "&lastmsgID=" + lastmsgid, function(newitems){
    newitems = convertTextEmo(newitems);
    api.getContentPane().append(newitems);  
});

Your function returns the modified HTML.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜