JS implements a plug-in that inserts picture text at the text box text field cursor (and the cursor is displayed after inserting content)

Source: Internet
Author: User

Js:

/*******************************************
*
* Plug-in at the insertion cursor
* @authors Du Xin Li
* @update 2015-10-25
*
*********************************************/

$.fn.extend ({
Insertcontent:function (myvalue, t) {
var = $ (this);
var $t = $ (this) [0];
if (document.selection) {
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 ();
}
}
})

How to use:

/**
* Click the emoticon button to insert the expression method
* @param {string} dom any child node
* @param {Object} Event event Object
*/
Fc.emote = Functi On (DOM, event) {
if (DOM) {
This.setparam (DOM);
} else {
return false;
}
var that = $ (DOM);
var e = window.event | | event;
if (e.stoppropagation) {
e.stoppropagation ();
} else{
E.cancelbubble = true;
}
//Show emoticon popup
$ ('. Fresh-dialog-emote '). Removeclass (' hide ');
//Current text box textarea
var _currenttextarea = This.param.form;
//Click the emoticon to insert the text box
$ ('. Fresh-dialog-emote '). Off (' click ', '. Fresh-jssmilies li '). On (' click ', '. Fresh-jssmilies li '), function () {
var _val = $ (this). Data (' action ');
Console.log (_val)
Console.log (_currenttextarea.length)
_currenttextarea.focus ();
_currenttextarea.insertcontent (_val);
$ ('. Fresh-dialog-emote '). addclass (' hide ');
})
}

Click on the Emoticons button to pop up the expression pop-up layer
$ ('. Fresh-list '). Off (' click ', '. fresh-comment-emote-btn '). On (' click ', '. Fresh-comment-emote-btn ', function (event) {
Fresh.comment.emote (this, event);
});

JS implements a plug-in that inserts picture text at the text box text field cursor (and the cursor is displayed after inserting content)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.