在textarea“onKeyUp”中禁用功能

时间:2017-05-04 19:06:48

标签: javascript html5 javascript-events

我正在尝试在test()代码中禁用textarea功能。

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()功能。

1 个答案:

答案 0 :(得分: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>

相关问题