在我的评论栏中,我希望用表情符号显示用户评论并显示。但textarea只支持纯文本,所以我在提交之前添加一个div来进行评论预览。
在我的工作中:
var smileys = {
':)': '<img src="smilies/smile.gif" border="0" alt="" />',
':-)': '<img src="smilies/smile.gif" border="0" alt="" />'
};
$(document).ready(function() {
$(".chat > textarea").bind("keyup", function(e) {
$(".chat > div").html(smilyMe($(".chat > textarea").val()));
});
});
function smilyMe(msg) {
//smiley replace
return msg.replace(/(\:\)|\:-\)|\:D|\:-D|\blol\b|\:-\||\:-\(|\;-\))/g, function(all) {
return smileys[all] || all;
});
}
div#maintbox {
border: 2px solid #ccc;
padding:0px;
overflow:auto;
width: 450px;
margin-bottom:3px;
}
div.chat {
width: 400px;
}
div.chat > div {
width: 400px;
color:#000000;
padding: 0px 0px 5px 0px;
z-index:997;
}
div.chat > textarea {
width: 400px;
background: transparent;
}
<div id="maintbox">
<div class="chat">
<div>
</div>
<textarea name="comment" rows="2" tabindex="4" id="comment" placeholder="Type here..."></textarea>
</div>
<div align="right" style="width:30px;float:right;"><img src="images/camera.png" width="25" alt="Upload image" id="uploadMedia" style="padding:2px;cursor:pointer;" /></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
我的问题
当我按Enter进入第2行并写入任何“文本”时,预览div不能在第2行显示“文本”,它在同一行显示“文本”。
答案 0 :(得分:0)
可以试试这个。首先添加<br>
然后休息:
var smileys = {
':)': '<img src="http://labs.google.com/ridefinder/images/mm_20_red.png" border="0" alt="" />',
':-)': '<img src="http://labs.google.com/ridefinder/images/mm_20_green.png" border="0" alt="" />'
};//images here are for demo add your images for use.
$(document).ready(function() {
$(".chat > textarea").bind("keyup", function(e) {
$(".chat > div").html(smilyMe($(".chat > textarea").val()));
});
});
function smilyMe(msg) {
msg=msg.replace(/(?:\r\n|\r|\n)/g, '<br />');
return msg.replace(/(\:\)|\:-\)|\:D|\:-D|\blol\b|\:-\||\:-\(|\;-\))/g, function(all) {
str= smileys[all] || all;
return str;
});
}
&#13;
div#maintbox {
border: 2px solid #ccc;
padding:0px;
overflow:auto;
width: 450px;
margin-bottom:3px;
}
div.chat {
width: 400px;
}
div.chat > div {
width: 400px;
color:#000000;
padding: 0px 0px 5px 0px;
z-index:997;
}
div.chat > textarea {
width: 400px;
background: transparent;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="maintbox">
<div class="chat">
<div>
</div>
<textarea name="comment" rows="2" tabindex="4" id="comment" placeholder="Type here..."></textarea>
</div>
<div align="right" style="width:30px;float:right;"><img src="images/camera.png" width="25" alt="Upload image" id="uploadMedia" style="padding:2px;cursor:pointer;" /></div>
</div>
&#13;