我有个按钮:
<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)";
}