项目中遇到金额输入框限制只能输入数字,输入特殊字符或者字母汉字时间隔不到1秒内容就会自动清空。跟正则纠缠多年的我初次见到,很是神奇-.-

代码实现:

<input type="text" onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo')"/>

看效果的话自己动手做做吧,没法上传动图。

下面着重介绍下 execCommand  

execCommand方法是执行一个对当前文档/当前选择/给出范围的命令。

处理Html数据时常用.
如下格式:document.execCommand(sCommand[,交互方式, 动态参数]) ,其中:sCommand为指令参数(如下例中的”2D-Position”),交互方式参数如果是true的话将显示对话框,如果为false的话,则不显示对话框(下例中的”false”即表示不显示对话框),动态参数一般为一可用值或属性值(如下例中的”true”)。

document.execCommand("2D-Position","false","true");
首先要说明的是在firefox下支持不好。
下面列出的是指令参数及意义(遇到的参数加高亮)
2D-Position 允许通过拖曳移动绝对定位的对象。 
AbsolutePosition 设定元素的 position 属性为“absolute”(绝对)。 
BackColor 设置或获取当前选中区的背景颜色。 
BlockDirLTR 目前尚未支持。 
BlockDirRTL 目前尚未支持。 
Bold 切换当前选中区的粗体显示与否。 
BrowseMode 目前尚未支持。 
Copy 将当前选中区复制到剪贴板。 
CreateBookmark 创建一个书签锚或获取当前选中区或插入点的书签锚的名称。 
CreateLink 在当前选中区上插入超级链接,或显示一个对话框允许用户指定要为当前选中区插入的超级链接的 URL。 
Cut 将当前选中区复制到剪贴板并删除之。 
Delete 删除当前选中区。 
DirLTR 目前尚未支持。 
DirRTL 目前尚未支持。 
EditMode 目前尚未支持。 
FontName 设置或获取当前选中区的字体。 
FontSize 设置或获取当前选中区的字体大小。 
ForeColor 设置或获取当前选中区的前景(文本)颜色。 
FormatBlock 设置当前块格式化标签。 
Indent 增加选中文本的缩进。 
InlineDirLTR 目前尚未支持。 
InlineDirRTL 目前尚未支持。 
InsertButton 用按钮控件覆盖当前选中区。 
InsertFieldset 用方框覆盖当前选中区。 
InsertHorizontalRule 用水平线覆盖当前选中区。 
InsertIFrame 用内嵌框架覆盖当前选中区。 
InsertImage 用图像覆盖当前选中区。 
InsertInputButton 用按钮控件覆盖当前选中区。 
InsertInputCheckbox 用复选框控件覆盖当前选中区。 
InsertInputFileUpload 用文件上载控件覆盖当前选中区。 
InsertInputHidden 插入隐藏控件覆盖当前选中区。 
InsertInputImage 用图像控件覆盖当前选中区。 
InsertInputPassword 用密码控件覆盖当前选中区。 
InsertInputRadio 用单选钮控件覆盖当前选中区。 
InsertInputReset 用重置控件覆盖当前选中区。 
InsertInputSubmit 用提交控件覆盖当前选中区。 
InsertInputText 用文本控件覆盖当前选中区。 
InsertMarquee 用空字幕覆盖当前选中区。 
InsertOrderedList 切换当前选中区是编号列表还是常规格式化块。 
InsertParagraph 用换行覆盖当前选中区。 
InsertSelectDropdown 用下拉框控件覆盖当前选中区。 
InsertSelectListbox 用列表框控件覆盖当前选中区。 
InsertTextArea 用多行文本输入控件覆盖当前选中区。 
InsertUnorderedList 切换当前选中区是项目符号列表还是常规格式化块。 
Italic 切换当前选中区斜体显示与否。 
JustifyCenter 将当前选中区在所在格式化块置中。 
JustifyFull 目前尚未支持。 
JustifyLeft 将当前选中区所在格式化块左对齐。 
JustifyNone 目前尚未支持。 
JustifyRight 将当前选中区所在格式化块右对齐。 
LiveResize 迫使 MSHTML 编辑器在缩放或移动过程中持续更新元素外观,而不是只在移动或缩放完成后更新。 
MultipleSelection 允许当用户按住 Shift 或 Ctrl 键时一次选中多于一个站点可选元素。 
Open 打开。 
Outdent 减少选中区所在格式化块的缩进。 
OverWrite 切换文本状态的插入和覆盖。 
Paste 用剪贴板内容覆盖当前选中区。 
PlayImage 目前尚未支持。 
Print 打开打印对话框以便用户可以打印当前页。 
Redo 重做。 
Refresh 刷新当前文档。 
RemoveFormat 从当前选中区中删除格式化标签。 
RemoveParaFormat 目前尚未支持。 
SaveAs 将当前 Web 页面保存为文件。 
SelectAll 选中整个文档。 
SizeToControl 目前尚未支持。 
SizeToControlHeight 目前尚未支持。 
SizeToControlWidth 目前尚未支持。 
Stop 停止。 
StopImage 目前尚未支持。 
StrikeThrough 目前尚未支持。 
Subscript 目前尚未支持。 
Superscript 目前尚未支持。 
UnBookmark 从当前选中区中删除全部书签。 
Underline 切换当前选中区的下划线显示与否。 
Undo 撤消。 
Unlink 从当前选中区中删除全部超级链接。 
Unselect 清除当前选中区的选中状态。

举个栗子:

例1:
    isNaN是测试是否为数值型 ,限制输入只能为数值如:1981.121,允许最多有一个小数点

<input type="text" name="text" onkeyup="if(isNaN(value))execCommand('undo')" />

例2:

<input type=button value=剪切 onclick=document.execCommand('Cut')>
<input type=button value=拷贝 onclick=document.execCommand('Copy')>
<input type=button value=粘贴 onclick=document.execCommand('Paste')>
<input type=button value=撤消 onclick=document.execCommand('Undo')>
<input type=button value=重做 onclick=document.execCommand('Redo') id=button2 name=button2>
<input>
<input type=button value=删除 onclick=document.execCommand('Delete')>
<input type=button value=黑体 onclick=document.execCommand('Bold')>
<input type=button value=斜体 onclick=document.execCommand('Italic')>
<input type=button value=下划线 onclick=document.execCommand('Underline')>
<input type=button value=停止 onclick=document.execCommand('stop')>
<input type=button value=保存 onclick=document.execCommand('SaveAs')>
<input type=button value=另存为 onclick=document.execCommand('Saveas',false,'c:\\test.htm')>
<input type=button value=字体 onclick=document.execCommand('FontName',false,fn)>
<input type=button value=字体大小 onclick=document.execCommand('FontSize',false,fs)>
<input type=button value=刷新 onclick=document.execCommand('refresh',false,0)>

还有些栗子:

 /*
*第二个参数最好不要设置为TRUE,否则可能会执行不了
*/
//加粗
function jiacu()
{
document.execCommand("Bold",false,null);
}
//斜体
function xieti()
{
document.execCommand("Italic",false,null);
}
//下划线
function xiahua()
{
document.execCommand("Underline",false,null);
}
//删除线
function shanchu()
{
document.execCommand("StrikeThrough",false,null);
} //设置字体
function setFontName(param)
{
document.execCommand("FontName",false,param);
document.getElementById("fontUl").style.display="none";
}
//设置字体大小
function setFontSize(param)
{
document.execCommand("FontSize",false,param);
document.getElementById("sizeUl").style.display="none";
}
//设置字体颜色
function setFontColor(param)
{
document.execCommand("ForeColor",false,param)
document.getElementById("fontColor1").style.display="none";
}
//设置背景颜色
function setBackColor(param)
{
document.execCommand("BackColor",false,param)
document.getElementById("bgColor1").style.display="none";
}
//删除文字格式
function removeFormat()
{
document.execCommand("RemoveFormat",false,null);
}
//对齐方式
function duiqiway(param)
{
document.execCommand(param,false,null);
document.getElementById("duiqiUl").style.display="none";
}
//插入列表
function insertList(param) //不能实现
{
document.execCommand(param,false,null);
alert("暂时未实现");
document.getElementById("liebiaoUl").style.display="none";
}
//改变缩进
function changeIndent(param) //不能实现
{
document.execCommand(param,false,null);
alert("暂时未实现");
}
//链接 //不能实现,取消链接的命令只用于用createLink命令创建的链接
function setLink(param)
{
document.execCommand(param,false,"http://blog.csdn.net/u011043843"); //第三个参数为URL
alert("暂时未实现");
}
//表情
function insertBQ(param)
{
document.execCommand("InsertImage",false,param); //param为图片的url
document.getElementById("bqDiv").style.display="none";
} //段落
function parag(param)
{
document.execCommand("formatBlock",false,param);
document.getElementById("paraUl").style.display="none";
}

document.execCommand()的用法小记的更多相关文章

  1. 转载:document.execCommand()的用法小记

    首先要说明的是在firefox下支持不好.2D-Position 允许通过拖曳移动绝对定位的对象. AbsolutePosition 设定元素的 position 属性为“absolute”(绝对). ...

  2. JavaScript:document.execCommand()的用法

    document.execCommand()的用法小记 一.语法 execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用. 如下格式:document.ex ...

  3. Javascript中document.execCommand()的用法

    document.execCommand()方法处理Html数据时常用语法格式如下:document.execCommand(sCommand[,交互方式, 动态参数]) 其中:sCommand为指令 ...

  4. JS开发备忘笔记-- Javascript中document.execCommand()的用法

    document.execCommand()方法处理Html数据时常用语法格式如下:document.execCommand(sCommand[,交互方式, 动态参数]) 其中:sCommand为指令 ...

  5. document.execCommand

    document.execCommand 在firefox浏览器执行不好,但是在其他浏览器有时候使用会非常方便. 比如在input标签中使用: onkeyup="if(isNaN(value ...

  6. 简易博客编辑器:玩转document.execCommand命令

    xhEditor是基于jQuery开发的跨平台轻量可视化XHTML编辑器,基于LGPL开源协议发布.今天就把它山寨一下. 上几张图,看看效果: 先做出菜单部分:发现是一张背景图片,所以用图片映射的方法 ...

  7. 前端复制功能的若干 -- document.execCommand()

    最近涨停科技公司实习,由于backend基础太弱...强行前端了一把..搞了两周才搞下页面里copy的功能,期间有些琐碎,恐忘,记录在此. 目前copy主流有四种方式:ZeroClipboard,Cl ...

  8. js document.createElement()的用法 (转)

    document.createElement()的用法 分析代码时,发现自己的盲点--document.createElement(),冲浪一番,总结了点经验. document.createElem ...

  9. document.execCommand 常用的方法

    execCommand方法是执行一个对当前文档,当前选择或者给出范围的命令.处理Html数据时常用如下格式: document.execCommand(sCommand[,交互方式, 动态参数]) , ...

随机推荐

  1. Timus 1132 Square Root(二次剩余)

    http://acm.timus.ru/problem.aspx?space=1&num=1132 题意: 求 x^2 ≡ n mod p  p是质数 的 解 本题中n>=1 特判p=2 ...

  2. 算法实践——舞蹈链(Dancing Links)算法求解数独

    在“跳跃的舞者,舞蹈链(Dancing Links)算法——求解精确覆盖问题”一文中介绍了舞蹈链(Dancing Links)算法求解精确覆盖问题. 本文介绍该算法的实际运用,利用舞蹈链(Dancin ...

  3. Linux 命令详解(十二)Systemd 入门教程:使用定时器发送邮件(mail)

    Systemd 定时器教程:http://www.ruanyifeng.com/blog/2018/03/systemd-timer.html 一.定时任务 Systemd 定时任务:每小时发送一封电 ...

  4. Good, then we can start

  5. request.setCharacterEncoding()、response.setCharacterEncoding()的区别

    request.setCharacterEncoding()是你设置获得数据的编码方式.response.setCharacterEncoding()是你响应时设置的编码.response.setCo ...

  6. 【四】Java虚拟机内存区域初识

     一.线程独占区  1.程序计数器 程序计数器是一块处于线程独占区较小的内存空间,它可以看是当前线程所执行的字节码的行号指示器. 如果线程执行的是Java方法,这个计数器记录的是正在执行的虚拟机字节码 ...

  7. 服务器中同一个【ip:port】可以多次accept的问题

    一.多次bind的问题 服务器的[ip:port]被某套接字绑定成功后,在该绑定解除之前,同一个[ip:port],不能再次被其他套接字绑定,否则绑定失败 二.多次accept的问题 有外来连接时,若 ...

  8. UIScrollView嵌套的完美解决方案

    UIScrollView嵌套的完美解决方案 做iOS开发,不可避免的会遇到UIScrollView的嵌套问题,之前也曾遇到过,吭哧吭哧做完了,效果不理想,和产品大战好几回合,就那样了.不可避免的,又一 ...

  9. (9)EvenOddJump

    一.问题描述 一只青蛙从数组(A)的每一个元素向数组尾部跳动.跳动规则如下: 当奇数跳的时候,就是第1.3.5.7....次进行移动时候,移动规则A[i] <= A[j],  并且A[j] = ...

  10. Restful API学习Day4 - DRF版本控制和认证

    参考文档: Django REST framework基础:版本控制 Django REST framework基础:认证.权限.限制 为什么要有版本? 某些客户端 使用低版本只维护不开发新功能 v1 ...