我有一个按钮:变化的onclick用JavaScript函数动作
<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)";
}
我不是这个答案,因为你是专门询问有关香草JavaScript的,但你可能想看看jQuery。这是一个JavaScript库,可以帮助你做你想要在你的问题中做的事情。 – JasCav 2011-03-14 20:12:20
了解JQuery,只能在这种情况下使用Javascript。谢谢。 – sepiroth 2011-03-14 20:13:28
'onclick'属性应该全部小写。 https://developer.mozilla.org/en/DOM/element.onclick – 2011-03-14 20:15:33