我试图插入 a<b>
每当 on keyup
, a#
被检测到(如Facebook
)。我从textarea
, 中读取并复制到一个div
元素。
<div class="new_postAdDescription2" id="new_postAdDescription2" spellcheck='false' contenteditable='true'> </div>
<textarea name="description" id="new_postAdDescription" spellcheck='false'
class="new_postAdDescription" cols="30" rows="10" placeholder="Posto një shpallje" >
</textarea>
请查看我如何替换空格和新行。我也尝试
过(顺便说一句,如果我删除空格匹配,一切正常!)
$('#new_postAdDescription').keyup(function (e) {
var str = $('#new_postAdDescription').val();
str = str.replace(/ /g, ' ');
str = str.replace(/\n/g, '<br/> ');
str = str.replace(/( |<br\/> )#([a-zA-Z0-9]+)/g, "$1<b class='highlighterContent'>#$2</b>");
$('#new_postAdDescription2').html(str);
});
// 风格
<style>
.new_postAdDescription, .new_postAdDescription2{
position: relative;
float: left;
background-color: transparent;
border: 0;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
outline: 0;
width: 694px;
color:#1c1c1c;
resize: none;
margin: 10px;
height: 115px;
font-size:13px;
line-height:1.3;
direction: ltr;
word-wrap:break-word;
}
.new_postAdDescription2{
position:absolute;
color:transparent;
word-wrap:break-word;
direction: ltr;
text-align: left;
}
.highlighterContent{
position:relative;
font-weight:bold;
color:#333;
background-color:#f2f2f2;
-webkit-box-sizing: border-box; /* Safari/Chrome, other WebKit */
-moz-box-sizing: border-box; /* Firefox, other Gecko */
box-sizing: border-box; /* Opera/IE 8+ */
}
</style>
这不是最好的,但在 Chrome 上运行良好,但在 FF 和 IE9 上,当涉及到断字时,它会与文本重叠并丢失轨道。看屏幕截图
在FF
和IE9
在Chrome
: