Javascript:将表情符号文本插入文本字段,单击表情图像的列表/显示

时间:2015-07-07 14:49:50

标签: javascript jquery css

有人可以帮忙。这是我的脚本,它收集表情符号列表并将alt关键字呈现为在表情文本区域栏上绘制的相应笑脸,然后在用户点击进入时在预览div上呈现显示。 但是在单击笑脸图片时会弹出图片/图像的迭代列表,当您选择其中任何一个时,它会在文本字段上呈现绘图,但是在第二次无法渲染时单击它,只是给出了文本。图像的替代文字。 此外,当您点击发送时,绘制的样式将拒绝显示在div预览中。

<div class="chat">
    <div class="preview"></div><br>
      <textarea name="comment" rows="2" tabindex="4" id="comment" placeholder="Type here..."></textarea>
  </div>

下面列出了我到目前为止所做的工作。

<html>
<head><title>Welcome to select emoticons</title>
<script type='text/javascript' src='https://c0d3.googlecode.com/files/jquery-latest.pack.js'></script>
<script src="https://jquery-elastic.googlecode.com/svn-history/r30/trunk/jquery.elastic.source.js"></script>
<script>
var smileys = {
    ':)': '<img src="images/smileys/happy.png" border="0" alt="" />',
    ':-)': '<img src="images/smileys/sad.png" border="0" alt="" />',
    ':D': '<img src="images/smileys/Cool.png" border="0" alt="" />',
};

function smilyMe(msg) {
    msg = msg.replace(/(?:\r\n|\r|\n)/g, '<br />');
    for (var key in smileys) {
        msg = msg.replace(key, smileys[key]);
    }
    return msg;
}
$(document).ready(function() {
  $(".chat > textarea").bind("keyup", function(e) {
    $(".chat > div").html(smilyMe($(".chat > textarea").val()));
  });
  $('#maintbox').on('keyup',function() {
    $(this).css('height','auto');
    $(this).css('height',Math.max(this.scrollHeight)+'px');
  });
  $('#showhide_emobox').click(function(){
    $('#emobox').fadeToggle();
    $(this).toggleClass('active');
  });
  $(".embtn").click(function(event){
    var prevMsg = $(".chat > textarea").val();
    var emotiText = $(event.target).attr("alt");
    $(".chat > textarea").html(prevMsg + emotiText);
     $(".chat > div").html(smilyMe($(".chat > textarea").val()));
  });
    $('textarea').elastic();
});
</script>
<style>
#maintbox {
    position:relative;
    border: 2px solid #ccc;
    min-height: 50px;
    width: 450;
    top:100px;
}
div.chat {
    width: 100%;
}
.preview {
    max-width: 400px;
    font-family:Times New Roman, Times, serif;
    font-size:14px;
    color:#000000;
    position:absolute;
    top:0; left:0; z-index:997;
}
div.chat > textarea {
    font-family:Times New Roman, Times, serif;
    font-size:14px;
    margin-left:-2px;
    margin-top:-2px;
    margin-bottom:2px;
    min-height: 50px;
    width: 510px;
    resize:none;
    float:left;
    position:absolute; color:transparent;
    top:0; left:0; z-index:998; background: transparent;
}
.em {
    position:relative;
    margin:1px 2px;
    float:right;
    height:20px;
    width:20px;
    text-align:center;
}
.em img {
    margin-top:1px;
    cursor:pointer;
    color:white;
    display:block;
    transition:all 1s ease;
}
#emobox {
    background:#fff;
    display:none;
    position:absolute;
    width:200px;
    bottom:25px;
    left:-176px;
    border: 2px solid #ccc;
    white-space:nowrap;
}
#emobox img {
    float:left;
    position:relative;
    display:inline-block;
    margin:5px 5px;
}
</style>
</head>
<body>
<div id="maintbox">
  <div class="chat">
    <div class="preview"></div><br>
      <textarea name="comment" rows="2" tabindex="4" id="comment" placeholder="Type here..."></textarea>
  </div>
 </div>    
<div align="right">
<img src="https://cdn3.iconfinder.com/data/icons/fez/512/FEZ-05-128.png" width="25" alt="" id="uploadMedia" style="padding:2px;cursor:pointer;" />
  <div class="em">
    <img src="http://simpleicon.com/wp-content/uploads/big-smile-256x256.png" width="25" id="showhide_emobox"/>
      <div id="emobox">
        <img src="images/smileys/happy.png" alt=":)" class="embtn" />
        <img src="images/smileys/sad.png" alt=":-)" class="embtn" />
        <img src="images/smileys/Cool.png" alt=":D" class="embtn" />
      </div>
  </div>
</div>
</body>
</html>

有人可以帮忙!

链接到小提琴:https://jsfiddle.net/872zb7gc

0 个答案:

没有答案