DIV CSS 佈局教程網

用 JavaScript 打造 @提及好友的自動提示(相容各大瀏覽器)
編輯:JavaScript綜合知識     

在社群輸入框裡輸入 @ 就跳出好友清單,是被用爛了的經典互動。本文示範如何以 JavaScript 打造這個特效,靈感取自百度貼吧,並針對 IE、Firefox 等瀏覽器做了相容處理。

需求

1. 輸入 @ 時,彈出匹配的好友選單
2. 游標進入包含「@好友」的標籤時,彈出選單
3. 按 backspace 刪除時,若游標前面是「@好友」標籤,彈出選單
4. 相容 IE、Firefox

具體做法

針對需求一,很自然會想到為輸入框綁定事件。這裡要綁 mousedown 而非 mouseup——因為若是 mouseup,用 event.preventDefault() 將無法阻止鍵盤輸入 @;而且在事件回呼裡 return false 也起不了作用。

綁定 mousedown 後,就要在游標處插入自訂的「@好友」標籤。新浪微博的輸入框是用 textarea 做的,無從得知內部如何處理,於是參考百度貼吧。

可以看到,貼吧插入了 <span class='at'></span> 標籤,這應該是為了方便後台用正規表達式匹配。

具體的

 

代碼如下:
vm.check_key=function(e){
var editor=$('editor'),range;
if(e.shiftKey&&e.keyCode==50){
if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.pasteHTML(" <span id='at"+at_index+"' class='at_span'>@</span> ");
}else{
document.execCommand("insertHtml", false," <span id='at"+at_index+"' class='at_span'>@</span> ");
}
e.preventDefault();
}
};

 

這裡需要在游標處插入,所以用到了 range。

接著是選單顯示,關鍵在於如何定位。作者的做法是為插入的 span 加上 id,再依 span 的位置為選單定位。若有更好的做法,歡迎分享。

具體的

 

代碼如下:
function at_box_show(at){
var at_pos=avalon($(at)).position();
$('at_box').style.left=at_pos.left+'px';
$('at_box').style.top=at_pos.top+16+'px';
$('at_box').style.display='block';
}
var at_index=0,cur_index=0;
avalon.define('editor', function(vm) {
vm.item_click=function(){
$('at'+cur_index).innerHTML="@"+this.innerHTML;
$('at_box').style.display='none';
at_index++;
};
vm.check_key=function(e){
var editor=$('editor'),a=getCharacterPrecedingCaret(editor),range;
if(e.shiftKey&&e.keyCode==50){
if (document.selection && document.selection.createRange) {
range = document.selection.createRange();
range.pasteHTML(" <span id='at"+at_index+"' class='at_span'>@</span> ");
}else{
document.execCommand("insertHtml", false," <span id='at"+at_index+"' class='at_span'>@</span> ");
}
at_box_show('at'+at_index);
cur_index=at_index;
e.preventDefault();
}
}
});

 

at_box_show 依新插入的 span id 為 at_box 定位並顯示選單。cur_index 表示游標目前所在的 span id——之所以要設這個變數,是因為使用者可能回頭去改先前插入的 span,而 at_index 是一直遞增的,因此需要另一個變數來記住當前位置。使用者點擊選單中的好友項時觸發 item_click,把好友名稱用 innerHTML 塞進當前 span,再隱藏選單並 at_index++。

以上是監聽 shift+@,接著是監聽 backspace 刪除。

 

代碼如下:
function getTextBeforeCursor(containerEl) {
var precedingChar = "", sel, range, precedingRange;
if (window.getSelection) {
sel = window.getSelection();
if (sel.rangeCount > 0) {
range = sel.getRangeAt(0).cloneRange();
range.collapse(true);
range.setStart(containerEl, 0);
precedingChar = range.cloneContents();
}
} else if ( (sel = document.selection)) {
range = sel.createRange();
precedingRange = range.duplicate();
precedingRange.moveToElementText(containerEl);
precedingRange.setEndPoint("EndToStart", range);
precedingChar = precedingRange.htmlText;
}
return precedingChar;
}

 

getTextBeforeCursor 的作用是取得游標前的內容。由於相容性,這個函式在標準瀏覽器中能得到游標前所有內容的 DocumentFragment,而在 IE 中只能得到文本;不過這段 HTML 字串可以轉成 DocumentFragment——avalon 用 parseHTML、jQuery 用 $(html)[0] 都能把 HTML 字串變成節點。

有了這個函式,再用 lastChild 就能判斷游標是否在游標前 HTML 的 lastChild 裡,且這個 lastChild 是 span。

具體的

 

代碼如下:
var a=getTextBeforeCursor($('editor'));
if(e.keyCode==8){
if(!-[1,]){
var b=avalon.parseHTML(a).lastChild;
}else{
var b=a.lastChild;
}
if(b.nodeType==1&&b.nodeName=='SPAN'){
var id=b.id;
cur_index=b.id.substring(2);
at_box_show(b.id);
}else
$('at_box').style.display='none';
}

 

最後是游標進入 span 標籤時顯示選單,這顯然需要綁定滑鼠事件。這裡綁 mouseup,因為若綁 mousedown,會需要滑鼠在 span 上再點一次才顯示。原理與上面差不多。

 

代碼如下:
vm.check_mouse=function(e){
var editor=$('editor'),a=getTextBeforeCursor(editor);
if(!-[1,]){
var b=avalon.parseHTML(getTextBeforeCursor(editor)).lastChild;
}else{
var b=a.lastChild;
}
if(b!=null&&b.nodeType==1&&b.nodeName=='SPAN'){
var id=b.id;
cur_index=b.id.substring(2);
at_box_show(b.id);
}else
$('at_box').style.display='none';
};

 

注意,若游標在 span 裡面,就要取出它的 id,讓 at_box 依此定位,另外還要重置 cur_index。至於 ajax 更新選單、字元匹配,這裡就不多贅述。

效果

firefox

ie8

ie7

ie6

下載

以上就是本文所述的全部內容了,希望能幫助你了解 JavaScript。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved