日期:2014-05-16 浏览次数:20363 次
$('#month').blur(function() { var va = $("#month").attr("value"); if (va != "" && !/^\d+(.\d{1,2})?$/.test(va)) { window.alert('月份输入错误!'); $("#month").focus(function(){ $("#month").val($("#month").val()); }); $("#month").focus(); return false; } });
<!DOCTYPE html> <html> <head> <meta charset=utf-8 /> <meta name="author" content="http://weibo.com/zswang" /> <title>Demo 示例控制输入光标位置</title> <style> </style> </head> <body> <input id="editor" type="text" value="1234" /> <input id="left" type="button" value="left" > <input id="right" type="button" value="right" > <script> void function(){ function setSelection(editor, pos){ if (editor.setSelectionRange){ editor.focus(); editor.setSelectionRange(pos, pos); } else if (editor.createTextRange){ var textRange = editor.createTextRange(); textRange.collapse(true); textRange.moveEnd("character", pos); textRange.moveStart("character", pos); textRange.select(); } } var editor = document.getElementById('editor'); document.getElementById('left').onclick = function(){ setSelection(editor, 0); } document.getElementById('right').onclick = function(){ setSelection(editor, editor.value.length); } }(); </script> </body> </html>