2017-05-04 60 views
0

我試圖在textarea標記中禁用test()函數。禁用textarea中的函數「onKeyUp」

onKeyUp="test()" // Not textarea 

<html> 
 
<head> 
 
</head> 
 
<body> 
 
<textarea id="aaa" onKeyUp="test()"></textarea> 
 
<br /> 
 
<input type="button" onclick="disable()" value="Disable" /> 
 
<input type="button" onclick="enable()" value="Enable" /> 
 

 

 
<script type="text/javascript"> 
 
function disable(){document.getElementById('aaa').disabled=true;} 
 
function enable(){document.getElementById('aaa').disabled=false;} 
 
</script> 
 

 
<script type="text/javascript"> 
 
function test(){} 
 
</script> 
 

 
</body> 
 
</html>

如何禁用和啓用此功能test()

回答

0

您可以使用.setAttribute添加和刪除所選元素的任何屬性的值,包括onKeyUp

要禁用:

// Instead of using: 
document.getElementById('aaa').disabled=true; 

// try: 
document.getElementById('aaa').setAttribute("onKeyUp", ""); 

要啓用:

// Instead of using: 
document.getElementById('aaa').disabled=false; 

// try: 
document.getElementById('aaa').setAttribute("onKeyUp", "test()"); 

檢查片段:

<html> 
 
<head> 
 
</head> 
 
<body> 
 
<textarea id="aaa" onKeyUp="test()"></textarea> 
 
<br /> 
 
<input type="button" onclick="disable()" value="Disable" /> 
 
<input type="button" onclick="enable()" value="Enable" /> 
 

 

 
<script type="text/javascript"> 
 
    function disable() { 
 
    document.getElementById('aaa').setAttribute("onKeyUp", ""); 
 
    } 
 

 
function enable() { 
 
    document.getElementById('aaa').setAttribute("onKeyUp", "test()"); 
 
    } 
 
</script> 
 

 
<script type="text/javascript"> 
 
    function test() { 
 
    alert("enabled"); 
 
    } 
 
</script> 
 

 
</body> 
 
</html>

+0

感謝您的幫助:) – moon93