JS输入编辑框实现输入@提示@人

1、输入编辑框当输入@的时候弹出提示,类似QQ聊天输入@人的效果

<div contenteditable=true></div>

2、输入效果:

效果

阅读 4.4k
3 个回答

推荐一个我曾经贡献过代码的jquery插件(23333,也只是帮他们改了个Bug)

https://github.com/ichord/At.js
demo
Features Preview

Support IE 7+ for textarea. Supports HTML5 contentEditable elements
(NOT including IE 8) Can listen to any character and not just '@'. Can
set up multiple listeners for different characters with different
behavior and data Listener events can be bound to multiple inputors.
Format returned data using templates Keyboard controls in addition to
mouse Tab or Enter keys select the value Up and Down navigate between
values (and Ctrl-P and Ctrl-N also) Right and left will re-search the

  1. Custom data handlers and template renderers using a group of

  2. callbacks Supports AMD

我也不知道qq那个到底是怎样实现的,如果不考虑动态数据的话,静态页面就监听输入框输入onchange事件,正则匹配"@"字符,然后显示提示框,提示框的效果就不用说了吧,当然,如果你需要提示框刚好在你@字符下面,这就要需要遍历字符串+定位了,大概是这样的一个思路了,希望对你有帮助

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题