JQuery在光标位置插入内容的实现代码 - Web前端
作者:98wpeu发布时间:2026-06-15分类:网页前端技术浏览:2
导读:复制代码代码如下:(function($){$.fn.extend({insertAtCaret:function(myvalue){var$t=$(this)[0];...
复制代码 代码如下:
(function($){
$.fn.extend({
insertAtCaret: function(myvalue){
var $t=$(this)[0];
if (document.selection) {
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else
if ($t.selectionStart || $t.selectionStart == '0') {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.subString(endPos, $t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
}
else {
this.value += myValue;
this.focus();
}
}
})
})(JQuery);
使用方法:
复制代码 代码如下: $(selector).insertAtCaret("value");
(function($){
$.fn.extend({
insertAtCaret: function(myvalue){
var $t=$(this)[0];
if (document.selection) {
this.focus();
sel = document.selection.createRange();
sel.text = myValue;
this.focus();
}
else
if ($t.selectionStart || $t.selectionStart == '0') {
var startPos = $t.selectionStart;
var endPos = $t.selectionEnd;
var scrollTop = $t.scrollTop;
$t.value = $t.value.substring(0, startPos) + myValue + $t.value.subString(endPos, $t.value.length);
this.focus();
$t.selectionStart = startPos + myValue.length;
$t.selectionEnd = startPos + myValue.length;
$t.scrollTop = scrollTop;
}
else {
this.value += myValue;
this.focus();
}
}
})
})(JQuery);
使用方法:
复制代码 代码如下: $(selector).insertAtCaret("value");
相关推荐
- jQuery getJSON 处理json数据的代码 - Web前端
- Jquery+WebService 校验账号是否已被注册的代码 - Web前端
- jQuery(1.6.3) 中css方法对浮动的实现缺陷分析 - Web前端
- jQueryPad 实用的jQuery测试工具(支持IE,chrome,FF) - Web前端
- 锋利的jQuery 要点归纳(二) jQuery中的DOM操作(下) - Web前端
- Jquery调用webService远程访问出错的解决方法 - Web前端
- jquery 打开窗口返回值实现代码 - Web前端
- 使用jquery hover事件实现表格的隔行换色功能示例 - Web前端
- 用jQuery简化Ajax开发实现方法第1/2页 - Web前端
- jQuery取得select选择的文本与值的示例 - Web前端
- 网页前端技术排行
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


