我有一个按钮:
<button id="a" onclick="Foo()">Button A</button>
当我第一次单击此按钮时,我希望它执行Foo(可以正确执行):
function Foo() {
document.getElementById("a").onclick = Bar();
}
第一次单击按钮时,我想发生的事情是将onclick函数从更改Foo()为Bar()。到目前为止,我只能实现无限循环或完全没有变化。Bar()看起来像这样:
function Bar() {
document.getElementById("a").onclick = Foo();
}
因此,单击此按钮只是交替调用哪个函数。我该如何工作?另外,有什么更好的方式显示/隐藏帖子的全文?它最初开始短路,我提供一个按钮来“查看全文”。但是,当我单击该按钮时,我希望用户能够再次单击该按钮以使长文本消失。
这是完整的代码,如果有帮助的话:
function ShowError(id) {
document.getElementById(id).className = document.getElementById(id).className.replace(/\bheight_limited\b/, '');
document.getElementById(id+"Text").className = document.getElementById(id+"Text").className.replace(/\bheight_limited\b/, '');
document.getElementById(id+"Button").innerHTML = "HIDE FULL ERROR";
document.getElementById(id+"Button").onclick = HideError(id);
}
function HideError(id) {
document.getElementById(id).className += " height_limited";
document.getElementById(id+"Text").className += " height_limited";
document.getElementById(id+"Button").innerHTML = "SHOW FULL ERROR";
document.getElementById(id+"Button").onclick = "ShowError(id)";
}