【发布时间】:2014-01-11 22:13:20
【问题描述】:
我搜索了很多,但找不到答案...
我有一个报价列表,每次单击按钮时,我都希望它转到新报价。 有人可以解释一下这里出了什么问题以及我应该如何解决它?
<script language="Javascript">
function buttonClickHandler() {
var textField = document.getElementById("textField");
var quotes = new Array();
var nextQuote = 0;
quotes[0] = "Don't be so humble - you are not that great.";
quotes[1] = "Moral indignation is jealousy with a halo.";
quotes[2] = "Glory is fleeting, but obscurity is forever.";
quotes[3] = "The fundamental cause of trouble in the world is that the stupid are cocksure while the intelligent are full of doubt.";
quotes[4] = "Victory goes to the player who makes the next-to-last mistake.";
quotes[5] = "His ignorance is encyclopedic";
quotes[6] = "If a man does his best, what else is there?";
quotes[7] = "Political correctness is tyranny with manners.";
quotes[8] = "You can avoid reality, but you cannot avoid the consequences of avoiding reality.";
quotes[9] = "When one person suffers from a delusion it is called insanity; when many people suffer from a delusion it is called religion.";
nextQuote++;
textField.value = quotes[nextQuote];
}
</script>
我在互联网上找到了此代码,当我使用此代码时,它会在每次点击时更改文本。
var currentValue = parseInt(textField.value);
// Add one
currentValue++;
// Put it back with the new +1'd value
textField.value = currentValue;
var quotes = new Array();
我用于数组的代码几乎相同,但不会更改每次点击的文本。我需要为数组做些什么特别的事情吗?帮助!!
【问题讨论】:
-
您可以无限期地使用
pop和push的组合来旋转引号而不是维护计数器。 -
你怎么打电话给
buttonClickHandler?如果您每次都调用它,它会始终显示您的第一个报价,因为您每次都在初始化nextQuote。 -
每次运行点击处理程序时,您都将
nextQuote重置为零。这使得报价 1 到 9 超重行李。 -
我的意思是
shift和push,基本上是移位一个值,将它存储在一个var中,然后将它推到最后。
标签: javascript arrays button text