页面中插入QQ表情

来源:互联网 发布:数据备份 方案 编辑:程序博客网 时间:2024/05/21 09:03

在页面中插入QQ表情。用的是一款jquery插件:jquery.qqFace.js。
1.页面引入jquery+jquery.qqFace.js两个js文件

<script type="text/javascript" src="jquery-1.7.2.min.js"></script> <script type="text/javascript" src="jquery.qqFace.js"></script> 

2.在body中写下如下:

<div id="show"></div> <div class="comment">     <div class="com_form">         <textarea class="input" id="saytext" name="saytext"></textarea>         <p><span class="emotion">表情</span><input type="button" class="sub_btn" value="提交"></p>     </div> </div> 

3.CSS:

.comment{width:680px; margin:20px auto; position:relative} .comment h3{height:28px; line-height:28px} .com_form{width:100%; position:relative} .input{width:99%; height:60px; border:1px solid #ccc} .com_form p{height:28px; line-height:28px; position:relative} span.emotion{width:42px; height:20px; background:url(icon.gif) no-repeat 2px 2px;  padding-left:20px; cursor:pointer} span.emotion:hover{background-position:2px -28px} .qqFace{margin-top:4px;background:#fff;padding:2px;border:1px #dfe6f6 solid;} .qqFace table td{padding:0px;} .qqFace table td img{cursor:pointer;border:1px #fff solid;} .qqFace table td img:hover{border:1px #0066cc solid;} #show{width:680px; margin:20px auto} 

4.现在样式搞定了,开始写JS,调用QQ表情:

$(function(){     $('.emotion').qqFace({         assign:'saytext', //给输入框赋值         path:'face/'    //表情图片存放的路径     });     ... }); 

5.当选择表情图片后,输入框中会插入一段如[em_5]之类的代码,代表插入的表情图片,实际应用中,点提交按钮后应该将这段表情代码连同其他内容插入到数据表中。而在页面显示的时候,我们应该将表情代码替换成真正的图片显示在页面上。下面的代码是插入表情图片后,点击提交按钮,使用javascript自定义函数将表情代码替换并显示:

$(function(){     ...     $(".sub_btn").click(function(){         var str = $("#saytext").val();         $("#show").html(replace_em(str));     }); }); function replace_em(str){     str = str.replace(/\</g,'<;');     str = str.replace(/\>/g,'>;');     str = str.replace(/\n/g,'<;br/>;');     str = str.replace(/\[em_([0-9]*)\]/g,'<img src="face/$1.gif" border="0" />');     return str; } 

6.如果您想用PHP代码来正则替换表情图片的话,可以使用以下函数(我自己没用php写过,所以下边的是我在文档中看的,直接给大家贴过来了。):

function ubbReplace($str){     $str = str_replace(">",'<;',$str);     $str = str_replace(">",'>;',$str);     $str = str_replace("\n",'>;br/>;',$str);     $str = preg_replace("[\[em_([0-9]*)\]]",">img src=\"face/$1.gif\" />",$str);     return $str; } 

另外,你如果想修改表情的显示位置,可以在jquery.qqFace.js中进行修改。具体修改哪些东西,还得根据自己的需要。

0 0
原创粉丝点击