文章出處

由于目前在接觸聊天室項目,接到一個需求實現一個類似QQ表情。在網上找了一款jquery經典表情插件。下面介紹一個用法:

HTML:

  首先在html頁面的head中引入jQuery庫文件和QQ表情插件jquery.qqFace.js文件

       <script type="text/javascript" src="jquery-1.7.2.min.js"></script> 
   <script type="text/javascript" src="jquery.qqFace.js"></script>
然后在body中加入以下html代碼:

1   <div id="show"></div> 
2     <div class="comment"> 
3     <div class="com_form"> 
4         <textarea class="input" id="saytext" name="saytext"></textarea> 
5         <p><span class="emotion">表情</span><input type="button" class="sub_btn" value="提交"></p> 
6     </div> 
7   </div> 

CSS:

我們用CSS來美化頁面,關鍵是表情按鈕圖片span.emotion的鼠標滑上與移開效果,以及調用表情插件后,顯示的表情.qqFace面板效果,請看代碼:

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

jquery:

當我們點擊頁面輸入框下方那個笑臉時,觸發調用qqface表情插件,簡單幾行就搞定。

1 $(function(){ 
2     $('.emotion').qqFace({ 
3 
4         assign:'saytext', //給輸入框賦值 
5         path:'face/'    //表情圖片存放的路徑 
6     }); 
7     ... 
8 }); 

當選擇表情圖片后,輸入框中會插入一段如[em_5]之類的代碼,代表插入的表情圖片,實際應用中,點提交按鈕后應該將這段表情代碼連同其他內容插入到數據表中。而在頁面顯示的時候,我們應該將表情代碼替換成真正的圖片顯示在頁面上。下面的代碼是插入表情圖片后,點擊提交按鈕,使用javascript自定義函數將表情代碼替換并顯示:

 1 $(function(){ 
 2     ... 
 3     $(".sub_btn").click(function(){ 
 4         var str = $("#saytext").val(); 
 5         $("#show").html(replace_em(str)); 
 6     }); 
 7 }); 
 8 function replace_em(str){ 
 9     str = str.replace(/\</g,'<;'); 
10     str = str.replace(/\>/g,'>;'); 
11     str = str.replace(/\n/g,'<;br/>;'); 
12     str = str.replace(/\[em_([0-9]*)\]/g,'<img src="face/$1.gif" border="0" />'); 
13     return str; 
14 } 

 

下面著重介紹下在nodejs+express4中的應用:

步驟如下:

1:加入引用表情的span

1         <div class="send-out">
2             <span class="emotion" id="emotion"><img class="head_picture" src="/images/r-middle-pic07.png">表情</span>
3             <input class="send-text" id="Y_iSend_Input" type="text" maxlength="100" value="">
4             <input id="Y_iSend_Bt" class="out_text" onclick="CHAT.submit();" type="button"  value="發送"/>
5         </div>

2:加入頭文件并調用jquery

1 <script type="text/javascript" src="/js/jquery.qqFace.js"></script>
2  $(function(){
3             $('.emotion').qqFace({
4                 assign:'Y_iSend_Input', //給輸入框賦值
5                 path:'/face/'    //表情圖片存放的路徑
6            });
7     });

3:用正則替換輸入的[]表情

 
w.CHAT={
replace_em: function (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;
        },

        //提交聊天消息內容
        submit: function () {

            var str = $("#Y_iSend_Input").val();

            $("#Y_iSend_Input").val(CHAT.replace_em(str));

            var message_text = d.getElementById("Y_iSend_Input").value;
  }
}

4:顯示

 

nodejs聊天室,淺析。

 


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()