我正在尝试在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()
功能。
答案 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>