jquery lets textarea support ctrl+z "undo" functionality

Source: Internet
Author: User
Tags setinterval

The core code is just this little bit.

The code is as follows Copy Code
var log = [];
$ (function () {
var txt = window.setinterval (function () {
if (Log[log.length-1]!= $ ("#t"). Val ()) {
Log[log.length] = $ ("#t"). Val ();
}
}, 1500);
var Isctrl = false;
$ (document). KeyDown (function (e) {
if (E.which = = 17)
Isctrl = true;
if (E.which = = && Isctrl = = True) {
Log.pop ();
$ ("#t"). Val (Log[log.length-1]). blur ();
}
}). KeyUp (function (e) {
if (E.which = = 17)
Isctrl = false;
});
});

Complete instance

The code is as follows Copy Code

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<title></title>
<script src= "Http://z3f.me/js/jquery-1.6.2.min.js" type= "Text/javascript" ></script>
<script type= "Text/javascript" >
var log = [];
$ (function () {
var txt = window.setinterval (function () {
if (Log[log.length-1]!= $ ("#t"). Val ()) {
Log[log.length] = $ ("#t"). Val ();
}
}, 1500);
var Isctrl = false;
$ (document). KeyDown (function (e) {
if (E.which = = 17)
Isctrl = true;
if (E.which = = && Isctrl = = True) {
Log.pop ();
$ ("#t"). Val (Log[log.length-1]). blur ();
$ ("#s"). Val (Log[log.length-1]). blur ();
}
}). KeyUp (function (e) {
if (E.which = = 17)
Isctrl = false;
});
});
Function Show () {
$ ("#s"). Val ("");
for (var i = 0; i < log.length; i++) {
$ ("#s"). Val (log[i]+ "rn[" + i + "]rn-----------------------rn" +$ ("#s"). Val ());
}
}
</script>

<body>

<textarea id= "T" style= "width:400px; height:300px; " >
</textarea>

<button onclick= "Show ()" > Display history </button>
<textarea id= "s" style= "width:400px; height:300px; " >
</textarea>
Press Ctrl+z to undo one step at a
Exchange please add QQ 10590916
</body>

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.