Javascript 如何禁用除文本框以外的所有页面上的键
在我的网站上,我使用了以下代码:Javascript 如何禁用除文本框以外的所有页面上的键,javascript,java,html,web,key,Javascript,Java,Html,Web,Key,在我的网站上,我使用了以下代码: <script type='text/javascript'> document.onkeydown = function (e) { return false; } </script> <script type='text/javascript'> document.onkeyup = function (e) { return false; } &
<script type='text/javascript'>
document.onkeydown = function (e)
{
return false;
}
</script>
<script type='text/javascript'>
document.onkeyup = function (e)
{
return false;
}
</script>
document.onkeydown=函数(e)
{
返回false;
}
document.onkeyup=函数(e)
{
返回false;
}
然而,在我的站点标题中,我希望用户只能够在聊天文本框中键入内容,而不需要在其他地方键入内容。聊天室文本框的代码为:
<div style="-moz-user-select: none; -webkit-user-select: none; -ms-user-
select:none; user-select:none;-o-user-select:none;" unselectable="on"
onselectstart="return false;" onmousedown="return false;">
<script type='text/javascript'>
document.onkeydown = function (e)
{
return true;
}
</script>
<script type='text/javascript'>
document.onkeyup = function (e)
{
return true;
}
</script>
<script type="text/javascript" async> ;
(function(o,l,a,r,k,y)
{if(o.olark)return;
r="script";y=l.createElement(r);r=l.getElementsByTagName(r)[0];
y.async=1;y.src="//"+a;r.parentNode.insertBefore(y,r); y=o.olark=function()
{k.s.push(arguments);k.t.push(+new Date)}; y.extend=function(i,j)
{y("extend",i,j)}; y.identify=function(i){y("identify",k.i=i)};
y.configure=function(i,j){y("configure",i,j);k.c[i]=j}; k=y._={s:[],t:[+new
Date],c:{},l:a}; })(window,document,"static.olark.com/jsclient/loader.js");
/* custom configuration goes here (www.olark.com/documentation) */
olark.identify('2624-366-10-5413');
</script>
<script type='text/javascript'>
document.onkeydown = function (e) {
return false;
}
</script>
<script type='text/javascript'>
document.onkeyup = function (e)
{
return false;
}
</script>
document.onkeydown=函数(e)
{
返回true;
}
document.onkeyup=函数(e)
{
返回true;
}
;
(函数(o、l、a、r、k、y)
{如果(o.K)返回;
r=“script”y=l.createElement(r);r=l.getElementsByTagName(r)[0];
y、 async=1;y.src=“/”+a;r.parentNode.insertBefore(y,r);y=o.olark=function()
{k.s.push(参数);k.t.push(+new Date)};y.extend=函数(i,j)
{y(“extend”,i,j)};y.identify=函数(i){y(“identify”,k.i=i)};
y、 configure=function(i,j){y(“configure”,i,j);k.c[i]=j};k=y。{s:[],t:[+new
(窗口,文档,“static.olark.com/jsclient/loader.js”);
/*这里有自定义配置(www.olark.com/documentation)*/
OLAK.识别('2624-366-10-5413');
document.onkeydown=函数(e){
返回false;
}
document.onkeyup=函数(e)
{
返回false;
}
这是我到目前为止所写的完整代码。看看这个 JAVASCRIPT:-
document.onkeyup = function(e){
console.log(e.target);
if(e.target.tagName == "INPUT"){
console.log(e.target.type);
if(e.target.type == "text"){
alert("Can press the key");
return true;
}
}
alert("Can't press the key");
return false;
}
document.onkeydown = function(e){
if(e.target.tagName == "INPUT"){
console.log(e.target.type);
if(e.target.type == "text"){
alert("Can press the key");
return true;
}
}
alert("Can't press the key");
return false;
}
HTML:-
<input type="text" />
<textarea></textarea>
你说的其他地方是什么意思?页面上是否有其他不希望使用的输入字段?如果是的话,他们为什么会在那里?如果你不能删除它们,你就不能禁用它们吗?@BSMP我希望它们在任何地方都被禁用,但在聊天中除外。@javamaster是的,我也不明白你的意思。如果不使用其他输入字段,为什么页面上还有其他输入字段。请不要说您使用输入设置页面样式。您可以使用e.target属性,它将为您提供您按下该键的元素。