1 //模板控制 2 var preMark = "["; 3 var latterMark = "]"; 4 $("#new_desc").on("keydown", function (event) { 5 var valtxt = $(this).val(); 6 if (valtxt.indexOf(preMark) > -1 && valtxt.indexOf(latterMark) > -1) { 7 var p = posCursor(this.id); 8 var perindex = valtxt.lastIndexOf(preMark, p.start); 9 var index = valtxt.indexOf(latterMark, p.start); 10 if(!(valtxt.substring(p.start, p.end).indexOf(latterMark) == -1 11 && valtxt.substring(p.start, p.end).indexOf(preMark) == -1)){ 12 return false; 13 } 14 else if (event.keyCode == '8') { 15 //删除键只能删除括号里面 16 if (valtxt.substring(perindex + 1, index).length > 0 && valtxt.substring(perindex + 1, index).indexOf(latterMark) == -1 && valtxt.substring(perindex + 1, index).indexOf(preMark) == -1 && valtxt.substring(perindex + 1, index).indexOf(latterMark) <= valtxt.substring(perindex + 1, index).indexOf(preMark) && p.end > perindex + 1) { 17 return true; 18 } else { 19 return false; 20 } 21 } 22 //禁用删除、全选、[] 23 else if (event.keyCode == "46" || event.shiftKey!=true && (event.keyCode == "221" || event.keyCode == "219" )|| (event.ctrlKey) && (event.keyCode == 65)) { 24 return false; 25 } 26 //括号外禁用所有操作 27 else if (event.keyCode != "38" && event.keyCode != "40" && perindex != -1 && index != -1 && perindex < index && perindex + 1 <= p.start && index >= p.start && valtxt.substring(perindex + 1, index).indexOf(latterMark) == -1 && valtxt.substring(perindex + 1, index).indexOf(preMark) == -1) { 28 if (perindex + 1 == p.start && event.keyCode == 37) { 29 moveCursor(this.id, perindex + 1, perindex + 1); 30 } else if (index == p.end && event.keyCode == 39) { 31 moveCursor(this.id, index, index); 32 } else { 33 return true; 34 } 35 } else { 36 if (index == p.end && event.keyCode == "37" || perindex + 1 == p.start && event.keyCode == "39") { 37 return true; 38 } 39 return false; 40 } 41 } else { 42 return false; 43 } 44 }) 45 //禁用拖拽 46 .on("drop", function () { 47 return false; 48 }) 49 .select(function (event) { 50 var valtxt = $(this).val(); 51 if (valtxt.indexOf(preMark) > -1 && valtxt.indexOf(latterMark) > -1) { 52 var p = posCursor(this.id); //括号范围之内不做任何操作 53 var perindex = valtxt.lastIndexOf(preMark, p.start); 54 var index = valtxt.indexOf(latterMark, p.start); 55 if (valtxt.substring(perindex + 1, index).indexOf(latterMark) == -1 56 && valtxt.substring(perindex + 1, index).indexOf(preMark) == -1 57 && valtxt.substring(perindex + 1, index).indexOf(latterMark) <= valtxt.substring(perindex + 1, index).indexOf(preMark) 58 && p.end >= perindex + 1 59 && valtxt.substring(p.start, p.end).indexOf(latterMark) == -1 60 && valtxt.substring(p.start, p.end).indexOf(preMark) == -1 61 ) { 62 63 } else { 64 window.getSelection().removeAllRanges(); 65 } 66 } else { 67 return false; 68 } 69 }) 70 //移入自动定位到指定中括号内 71 .on("mouseup", function () { 72 var valtxt = $(this).val(); 73 if (valtxt.indexOf(preMark) > -1 && valtxt.indexOf(latterMark) > -1) { 74 var p = posCursor(this.id); 75 //括号范围之内不做任何操作 76 if (valtxt.lastIndexOf(preMark, p.start) < p.start && p.start < valtxt.indexOf(latterMark, p.start) + 1 && valtxt.substring(valtxt.lastIndexOf(preMark, p.start) + 1, valtxt.indexOf(latterMark, p.end)).indexOf(latterMark) == -1 && valtxt.substring(valtxt.lastIndexOf(preMark, p.start) + 1, valtxt.indexOf(latterMark, p.end)).indexOf(preMark) == -1) { 77 78 } else { 79 //定位并选中括号之间 80 var startindex = valtxt.indexOf(preMark, p.start); 81 if (startindex > -1) { 82 var endindex = valtxt.indexOf(latterMark, p.start); 83 var lstartindex = valtxt.lastIndexOf(preMark, p.start); 84 var lendindex = valtxt.lastIndexOf(latterMark, p.start); 85 if ((lstartindex > -1 && Math.abs(p.start - lendindex) < Math.abs(p.start - startindex)) && valtxt.substring(lendindex, p.start - 2).indexOf(" ") == -1) { 86 startindex = lstartindex; 87 endindex = lendindex; 88 } 89 } else { 90 startindex = valtxt.lastIndexOf(preMark, p.start); 91 var endindex = valtxt.lastIndexOf(latterMark, p.start); 92 } 93 // 94 var ncnt = 0; 95 //执行光标定位 96 moveCursor(this.id, startindex + 1 + ncnt, endindex + ncnt); 97 } 98 } else { $(this).blur(); } 99 });