admin管理员组文章数量:1425800
(function( $ ) {
$.fn.htmlCodes = function(){
var $this = this;
var body = this.html();
body = body.replace(':)', '☺').replace('<3', '♥');
this.html(body);
};
})( jQuery );
I wrote this JQuery plugin so that it would change smiley faces to the smiley face entity. (and heart)
However, the heart doesn't work! For some reason the smiley works but heart doesn't.
(function( $ ) {
$.fn.htmlCodes = function(){
var $this = this;
var body = this.html();
body = body.replace(':)', '☺').replace('<3', '♥');
this.html(body);
};
})( jQuery );
I wrote this JQuery plugin so that it would change smiley faces to the smiley face entity. (and heart)
However, the heart doesn't work! For some reason the smiley works but heart doesn't.
Share Improve this question asked Feb 21, 2012 at 3:23 TIMEXTIMEX 273k368 gold badges802 silver badges1.1k bronze badges3 Answers
Reset to default 10Works if you do this...
.replace('<3', '♥');
DEMO: http://jsfiddle/ZdJh5/
The .html()
is giving you the HTML character code for the <
symbol.
A <
is a special character in HTML, which is what jQuery's html
function will return, and so you need to replace <
instead of <
. Also, for a catch-all, you should use the /g
flag:
(function( $ ) {
$.fn.htmlCodes = function() {
var $this = this;
var body = this.html();
body = body.replace(/:\)/g, '☺').replace(/<3/g, '♥');
this.html(body);
};
})( jQuery );
Otherwise, only the first instance will be replaced. And a quick demo to show that it works. ☺
body = body.replace(':)', '☺').replace('<3', '♥');
It is possible <
gets encoded as <
.
本文标签: jqueryHow come this javascript does not replace text with the quotheartquot symbolStack Overflow
版权声明:本文标题:jquery - How come this javascript does not replace text with the "heart" symbol? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745413777a2657589.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论