禁用后无法启用文本区域



我想在按下按钮时禁用文本区域,并在按下另一个按钮时再次启用它。现在我可以禁用它,但我无法再次启用它。

HTML:

<textarea rows='14' id="value"> </textarea>
<button class="continue" onclick="return cont()">CONTINUE</button>
<button class="clear" onclick="return clear()">CLEAR</button>

JS:

function cont(){
document.getElementById("value").value = 'hello';
document.getElementById("value").readOnly = true;
setTimeout('cont()',1000);
}
function clear(){
document.getElementById("value").readOnly = false;
document.getElementById("value").value = 'empty';
setTimeout('clear()',1000);
}

为什么我的clear按钮不工作?

您可以这样做:

HTML:

<textarea rows='14' id="value"> </textarea>
<button class="continue">CONTINUE</button>
<button class="clear">CLEAR</button>

JS:

const continueButton = document.querySelector('.continue');
const clearButton = document.querySelector('.clear');
const textArea = document.querySelector('#value');

continueButton.addEventListener('click', function(e) {
textArea.value = 'hello'
textArea.disabled = true
});
clearButton.addEventListener('click', function(e) {
textArea.value = ''
textArea.disabled = false
});

setTimeout('clear()',1000)更改为setTimeout('clear',1000)

最新更新