【发布时间】:2011-09-15 20:05:49
【问题描述】:
我正在制作一个类似于 facebook 中的提及系统,现在我使用 textarea 实现了它,但现在我希望它与 contenteditable div 一起使用,以便我可以在其中格式化文本。
所以现在所做的是,当您按下“@”和一个字母时,它会使用 JQuery-UI 自动完成在数组中搜索包含该字母的结果,当您选择结果时,它会在 div 中显示结果,但现在我想强调像 facebook 这样的提及,所以我放了一个:
<b style="background: #somecolor"></b>
所以提及看起来像这样:
<b style="background: #somecolor">Mention</b>
到目前为止一切顺利,但是当我想继续写作时,我写的所有新文本都在 <b></b> 中,如下所示:
<b style="background: #somecolor">Mention all my new text</b>
而不是<b style="background: #somecolor">Mention</b> all my new text
所以所有新文本都被突出显示,这是一张图片:
http://dev.frinki.com/question/1.png
这是我正在使用的 javascript 代码
$("#Conversacion").bind("keydown", function(event) {
if (event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
event.preventDefault();
}
}).autocomplete({
minLength: 0,
source: function(request, response) {
var term = request.term,
results = [];
console.log(term);
if (term.indexOf("@") >= 0) {
term = extractLast(request.term);
if (term.length > 0) {
results = $.ui.autocomplete.filter(availableTagsFollowing, term);
} else {
//results = ['Start typing...'];
}
}
response(results);
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function(event, ui) {
var terms = split(this.innerHTML);
// remove the current input
terms.pop();
// add the selected item
terms.push('<b style="background:#E0FFC5">' + ui.item.label + '</b>');
this.innerHTML = terms.join("");
$('#mentionsHidden').val($('#mentionsHidden').val() + '@['+ui.item.value+':'+ui.item.label+']');
mentionsString = $('#mentionsHidden').val();
return false;
}
});
这是 HTML 代码
<div name="Descripcion" id="Conversacion" class="Conversacion" contenteditable="true"></div>
这是 Chrome 的 Inspector 的图片:
http://dev.frinki.com/question/2.png
我在 facebook 中查看了它是如何完成的,这是 chrome 的检查器在 facebook 中的图片:
3.png(其他两张图片的域和文件夹相同,抱歉,这是一个新用户,我不能发布超过2个链接)
当您选择要提及的人时,facebook 似乎会在它使用的 span 中创建一个新行,因此文本与 <b></b> 分开。
我希望有人可以帮助我。
顺便说一句,我很抱歉我的英语,我来自南美。
【问题讨论】:
标签: jquery facebook html autocomplete contenteditable