1

I am trying to figure out how facebook does the person-highlight tagging below.

I have a <textarea> and what I want is that ability when I press backspace the whole name gets deleted.

And I also wanted the name to be highlighted in blue (but this one is easy).

Can this be done easily through css? Or do I have to use some sort of javascript to have the deletion stuff working?

TLDR: I needed a jQuery function that deletes the name that is highlighted when I press back..that's all, no need for autocompletion and such

enter image description here

4

2 回答 2

1

You can use Jquery Token input https://github.com/loopj/jquery-tokeninput

More comprehensive list is here: Facebook style JQuery autocomplete plugin

于 2013-05-16T05:51:21.173 回答
1

you can make it by adding an absolute div behind the required text, and make the background transparent for the textarea.
here is a snippet of code that I just wrote, it might help you.
I just faced some problems with adding the correct left position to the highlighted div.

html :

<div class='container'>
  <div class='highlighted'></div>
  <textarea class='text_area'>hi my name is Ayman</textarea>
</div>

css :

.custom_table{
position: relative;
width:600px;
}

.row{
position: relative;
height:40px;
background: #c80000;
border-top:1px solid #fff;
}

.row div{
color:#fff;
text-align: center;
line-height:40px;
}
.row:hover .first,
.row:hover .second,
.row:hover .third,
.row:hover .fourth{
background:#522D2D;;
cursor:pointer;
}

.first{
position: absolute;
left:0%;
right:80%;
height:40px;
background: #00c800;
}

.second{
position: absolute;
left:20%;
right:60%;
height:40px;
background: #0000c8;
}

.third{
position: absolute;
left: 40%;
right: 25%;
height: 40px;
background: #BEBECF;
}

.fourth{
position: absolute;
left: 75%;
right: 0%;
height: 40px;
background: #D6182F;
}

JavsScript :

$(document).ready(function(e){
$('.text_area').bind('keypress', function(e) {
     var code = (e.keyCode ? e.keyCode : e.which);
    if (code == 64){
        $('.text_area').val($('.text_area').val()+" Ayman")
        line = $('.text_area').val().substr(0, $('.text_area').selectionStart).split("\n").length;
        var hl = $("<div class='highlighted'></div>");
        $(hl).css({'left':$('.text_area').getCursorPosition()+"px", 'top': ((line*14)-12)+"px"})
        $('.container').append($(hl))
    }
});
});

(function ($, undefined) {
$.fn.getCursorPosition = function() {
    var el = $(this).get(0);
    var pos = 0;
    if('selectionStart' in el) {
        pos = el.selectionStart;
    } else if('selection' in document) {
        el.focus();
        var Sel = document.selection.createRange();
        var SelLength = document.selection.createRange().text.length;
        Sel.moveStart('character', -el.value.length);
        pos = Sel.text.length - SelLength;
    }
    return pos;
}
})(jQuery);
于 2013-05-16T07:14:21.143 回答