jQuery往textarea中光標(biāo)所在位置插入文本的方法
本文實(shí)例講述了jQuery往textarea中光標(biāo)所在位置插入文本的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
<html>
<head>
<script src="jquery-1.8.1.min.js"></script>
<script >
$(function() {
/* 在textarea處插入文本--Start */
(function($) {
$.fn.extend({
insertContent : function(myValue, t) {
var $t = $(this)[0];
if (document.selection) { // ie
this.focus();
var sel = document.selection.createRange();
sel.text = myValue;
this.focus();
sel.moveStart('character', -l);
var wee = sel.text.length;
if (arguments.length == 2) {
var l = $t.value.length;
sel.moveEnd("character", wee + t);
t <= 0 ? sel.moveStart("character", wee - 2 * t - myValue.length) : sel.moveStart( "character", wee - t - myValue.length);
sel.select();
}
} 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;
if (arguments.length == 2) {
$t.setSelectionRange(startPos - t,
$t.selectionEnd + t);
this.focus();
}
} else {
this.value += myValue;
this.focus();
}
}
})
})(jQuery);
/* 在textarea處插入文本--Ending */
});
$(document).ready(function(){
$("#ch_button").click( function () {
$("#test_in").insertContent("<upload/day_140627/201406271546349972.jpg>");
});
});
</script>
</head>
<body >
<button id="ch_button" value="插入" >插入</button>
<textarea name="content" id="test_in" rows="30" cols="100">
</textarea>
</body>
</html>
希望本文所述對(duì)大家的jQuery程序設(shè)計(jì)有所幫助。
- jquery實(shí)現(xiàn)在光標(biāo)位置插入內(nèi)容的方法
- 基于jQuery的輸入框在光標(biāo)位置插入內(nèi)容, 并選中
- JQuery在光標(biāo)位置插入內(nèi)容的實(shí)現(xiàn)代碼
- jQuery 在光標(biāo)定位的地方插入文字的插件
- jQuery:節(jié)點(diǎn)(插入,復(fù)制,替換,刪除)操作
- 關(guān)于用Jquery的height()、width()計(jì)算動(dòng)態(tài)插入的IMG標(biāo)簽的寬高的問(wèn)題
- JQuery插入DOM節(jié)點(diǎn)的方法
- jQuery實(shí)現(xiàn)在最后一個(gè)元素之前插入新元素的方法
- jquery插入兄弟節(jié)點(diǎn)的操作方法
- jQuery?在圖片和文字中插入內(nèi)容實(shí)例
相關(guān)文章
jQuery(js)獲取文字寬度(顯示長(zhǎng)度)示例代碼
今天遇到了獲取文字寬度的問(wèn)題,在網(wǎng)上找到了不錯(cuò)的方法并成功使用到了項(xiàng)目中,有類似情況的朋友可以參考下2013-12-12
jquery dataview數(shù)據(jù)視圖插件使用方法
這篇文章主要介紹了jquery dataview數(shù)據(jù)視圖插件使用方法,數(shù)據(jù)填充與視圖更新利器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
jquery中each遍歷對(duì)象和數(shù)組示例
jquery中each可用于遍歷對(duì)象和數(shù)組,如需退出each循環(huán)可使回調(diào)函數(shù)返回false,下面有個(gè)示例,大家可以看看2014-08-08
jQuery.form.js插件不能解決連接超時(shí)(timeout)的原因分析及解決方法
jQuery.form.js是一個(gè)form插件,支持ajax表單提交和ajax文件上傳。最近在使用jquery.form.js提交包含文件的表單時(shí),當(dāng)碰上網(wǎng)速較慢時(shí),而我們又設(shè)置了timeout時(shí)我們的頁(yè)面會(huì)死在這里,怎么回事呢,下面腳本之家小編給大家解答下2016-10-10
jQuery實(shí)現(xiàn)預(yù)加載圖片的方法
這篇文章主要介紹了jQuery實(shí)現(xiàn)預(yù)加載圖片的方法,涉及jQuery操作img的src屬性使用技巧,需要的朋友可以參考下2015-03-03
實(shí)例代碼講解jquery easyui動(dòng)態(tài)tab頁(yè)
這篇文章主要介紹了實(shí)例代碼講解jquery easyui動(dòng)態(tài)tab頁(yè)的相關(guān)資料,需要的朋友可以參考下2015-11-11
jQuery實(shí)現(xiàn)發(fā)送驗(yàn)證碼并60秒倒計(jì)時(shí)功能
這篇文章主要介紹了jQuery實(shí)現(xiàn)發(fā)送驗(yàn)證碼并60秒倒計(jì)時(shí)功能,非常不錯(cuò),代碼簡(jiǎn)單易懂,需要的朋友參考下吧2016-11-11

