javascript - Replace only part of text in jQuery function -
i'm using solution replace list of emojies:
(function($){ var emojies = ["smile", "smiley", "grin", "joy"]; var emojiregex = new regexp(":("+ emojies.join("|") +"):","g"); $.fn.toemoji = function(){ return this.each(function() { this.innerhtml = this.innertext.replace(emojiregex,function(fullmatch,match){ return '<span class="emoji '+match.tolowercase()+'"></span>'; }); }); }; })(jquery); //and use $(document).ready(function(){ $(".content div").toemoji(); });
but replace content div (this.innerhtml...
), however, there no way , replace :emoji: , not text?
because, if text has break line, ex:
hi!
how you?
will replace for:
hi! how you?
in addition other problems... so, how do?
the problem reading div innertext
not include html tags <br/>
. instead, can read div innerhtml
so:
$.fn.toemoji = function(){ return this.each(function() { this.innerhtml = this.innerhtml.replace(emojiregex,function(fullmatch,match){ return '<span class="emoji '+match.tolowercase()+'"></span>'; }); }); };
note this.innerhtml.replace
instead of this.innertext.replace
!
jsfiddle: http://jsfiddle.net/ybj7gpn6/ (inspect html after clicking button see spans present -- looks blank space)
Comments
Post a Comment