我即将在我的contenteditable div中实现Facebook ,如果我给'$'和一些像'a'这样的字符我需要一个自动建议,它应该弹出我的插入位置附近.
我需要知道如何在JavaScript for IE和其他浏览器中找到插入位置之前的最后一个字符.我可以访问Jquery库.
(function($) {
$.fn.getCursorPosition = function() {
var input = this.get(0);
if (!input) return; // No (input) element found
if ('selectionStart' in input) {
// Standard-compliant browsers
return input.selectionStart;
} else if (document.selection) {
// IE
input.focus();
var sel = document.selection.createRange();
var selLen = document.selection.createRange().text.length;
sel.moveStart('character', -input.value.length);
return sel.text.length - selLen;
}
}
})(jQuery);
eg.
var caretPosition = $("#contenteditablediv").getCursorPosition();
var lastchar = getchar(caretposition -1);???
Run Code Online (Sandbox Code Playgroud) 下面的方法返回来自AZ和0-9的任何键,当我按下shift键+ 4时,它返回相同的代码52.
function keyUpEvent(e)//onkeyup event
{
var chr = String.fromCharCode(e.keyCode);//converts the keycode into a character
alert(chr);//alerts the character
}
Run Code Online (Sandbox Code Playgroud)
但是当按下shift键组合的相应键时,我需要显示$或%.
class Animal { }
class Tiger : Animal { }
class Lion : Animal { }
Run Code Online (Sandbox Code Playgroud)
将一些动物(来自父Animal类的狮子和老虎)添加到动物列表中.
class Program
{
static void Main(string[] args)
{
List<Animal> animals = new List<Animal>();
animals.Add(new Tiger());
animals.Add(new Tiger());
animals.Add(new Tiger());
animals.Add(new Lion());
animals.Add(new Lion());
animals.Add(new Lion());
List<Tiger> tigers = new List<Tiger>();
foreach (var item in animals){
if(item is Tiger){
var _tiger = item as Tiger;
tigers.Add(_tiger);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我会使用上面的foreach循环获取老虎列表,但如果老虎使用Linq,是否有任何实现来获取列表,
喜欢List<Tiger> tigers = animals.where(somePredicate)?