2017-06-03 75 views
0

在我的網站我用下面的代碼:如何在所有的頁面禁用鍵除了文本框

<script type='text/javascript'> 
    document.onkeydown = function (e) 
    { 
    return false; 
    } 
    </script> 

    <script type='text/javascript'> 
    document.onkeyup = function (e) 
    { 
    return false; 
    } 
</script> 

我的網站上標題,不過,我希望用戶只能夠輸入聊天文本框和其他地方。聊天文本框的代碼是:

<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> 

這是我迄今爲止編寫的完整代碼。

+3

你是什麼意思*無處*?你不希望他們使用頁面上的其他輸入字段嗎?如果是這樣,爲什麼他們在那裏呢?如果你不能刪除它們,你不能僅僅禁用它們嗎? – BSMP

+3

@BSMP我希望他們在任何地方都被禁用,但在聊天中。 – antimalwareprogram

+1

@javamaster是的,我不明白你的意思。爲什麼頁面上還有其他輸入字段,如果它們不被使用。請不要說你的輸入樣式是你的頁面。 – Darkrum

回答

0

看一看這個JSFIDDLE

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> 
+3

工作,謝謝 – antimalwareprogram

+1

如果它適合你,你可以接受答案。 – Mistletoe

相關問題