页面中插入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
- 页面中插入QQ表情
- android中edittext插入QQ表情后,无法使用输入法键盘的退格键(删除键)删除表情的解决方法
- Android---文字中插入表情
- Android---文字中插入表情
- Android---文字中插入表情
- Android---文字中插入表情
- 【Demo】EditText中插入表情
- EditText插入表情,模仿聊天页面
- QQ表情
- WEB项目中使用QQ表情
- 向EditView插入qq表情,并可删除表情或文字
- 在EditText中插入表情(图片)
- android textview和edittext中插入表情
- android textview和edittext中插入表情
- Android的EditView中插入表情图像
- 评论接口中插入表情,后台不支持
- NGUI聊天信息中插入动态表情
- 插入表情
- 第一次只出现一次的字符
- Unity3D学习日记(五)实现围绕物品旋转的的摄像机控制
- Android引导页设计
- javaScript 定时器
- Flexbox布局技术研究
- 页面中插入QQ表情
- C#的Infinity和NaN
- Hibernate进行模糊查询
- Cordova 环境搭建(Window)
- JavaScript上传图片前预览
- c语言格式字符串 % lf 和 %f
- 练习2————1014
- 如何处理Permission denied: user=root, access=WRITE, inode="/user" 这类Hadoop权限问题问题
- X264编码