javascript模拟键盘输入

今天在工作遇到一个问题跟大家分享一下:
需求是这样的,在一个B/S的系统中需要调用浏览器的“查找”功能,通常我们的操作是:使用快捷键“Ctrl+F”,但是我的要求是需要通过 JavaScript来主动激活。我先是通过document对象的方法execCommand查找,最后发现没有查找功能,但是它很有用,我把它总结如下:
常用Js代码document.execCommand()的作用:
<html>
<body>
<input type=button value=剪切 οnclick=document.execCommand('Cut')>
<input type=button value=拷贝 οnclick=document.execCommand('Copy')>
<input type=button value=粘贴 οnclick=document.execCommand('Paste')>
<input type=button value=撤消 οnclick=document.execCommand('Undo')>
<input type=button value=重做 οnclick=document.execCommand('Redo') id=button2 name=button2>
<input>
<input type=button value=删除 οnclick=document.execCommand('Delete')>
<input type=button value=黑体 οnclick=document.execCommand('Bold')>
<input type=button value=斜体 οnclick=document.execCommand('Italic')>
<input type=button value=下划线 οnclick=document.execCommand('Underline')>
<input type=button value=停止 οnclick=document.execCommand('stop')>
<input type=button value=保存 οnclick=document.execCommand('SaveAs')>
<input type=button value=另存为 οnclick=document.execCommand('Saveas',false,'c:\\test.htm')>
<input type=button value=字体 οnclick=document.execCommand('FontName',false,fn)>
<input type=button value=字体大小 οnclick=document.execCommand('FontSize',false,fs)>
<input type=button value=刷新 οnclick=document.execCommand('refresh',false,0)>
</body>
</html>
下面列出的是指令参数及意义:
//相当于单击文件中的打开按钮
document.execCommand("Open");
//将当前页面另存为
document.execCommand("SaveAs");
//剪贴选中的文字到剪贴板;
document.execCommand("Cut","false",null);
//删除选中的文字;
document.execCommand("Delete","false",null);
//改变选中区域的字体;
document.execCommand("FontName","false",sFontName);
//改变选中区域的字体大小;
document.execCommand("FontSize","false",sSize|iSize);
//设置前景颜色;
document.execCommand("ForeColor","false",sColor);
//使绝对定位的对象可直接拖动;
document.execCommand("2D-Position","false","true");
//使对象定位变成绝对定位;
document.execCommand("AbsolutePosition","false","true");
//设置背景颜色;
document.execCommand("BackColor","false",sColor);
//使选中区域的文字加粗;
document.execCommand("Bold","false",null);
//复制选中的文字到剪贴板;
document.execCommand("Copy","false",null);
//设置指定锚点为书签;
document.execCommand("CreateBookmark","false",sAnchorName);
//将选中文本变成超连接,若第二个参数为true,会出现参数设置对话框;
document.execCommand("CreateLink","false",sLinkURL);
//设置当前块的标签名;
document.execCommand("FormatBlock","false",sTagName);
//相当于单击文件中的打开按钮
document.execCommand("Open");
//将当前页面另存为
document.execCommand("SaveAs");
//剪贴选中的文字到剪贴板;
document.execCommand("Cut","false",null);
//删除选中的文字;
document.execCommand("Delete","false",null);
//改变选中区域的字体;
document.execCommand("FontName","false",sFontName);
//改变选中区域的字体大小;
document.execCommand("FontSize","false",sSize|iSize);
//设置前景颜色;
document.execCommand("ForeColor","false",sColor);
//使绝对定位的对象可直接拖动;
document.execCommand("2D-Position","false","true");
//使对象定位变成绝对定位;
document.execCommand("AbsolutePosition","false","true");
//设置背景颜色;
document.execCommand("BackColor","false",sColor);
//使选中区域的文字加粗;
document.execCommand("Bold","false",null);
//复制选中的文字到剪贴板;
document.execCommand("Copy","false",null);
//设置指定锚点为书签;
document.execCommand("CreateBookmark","false",sAnchorName);
//将选中文本变成超连接,若第二个参数为true,会出现参数设置对话框;
document.execCommand("CreateLink","false",sLinkURL);
//设置当前块的标签名;
document.execCommand("FormatBlock","false",sTagName);
知道此路不通,该道而行,解决方案如下:
<html>
<body>
<input type="button" value="查找" onClick="selecttool()"/>
</body>
</html>
<script language="javascript">
function selecttool(){
var WshShell = new ActiveXObject("Wscript.Shell");
try{WshShell.SendKeys("^f");} catch(e){}
WshShell.Quit;
}
</script>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值