首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每次单击按钮时更改文本

每次单击按钮时更改文本
EN

Stack Overflow用户
提问于 2014-01-11 22:13:20
回答 6查看 2.2K关注 0票数 0

我找了很多但找不到答案..。

我有一个引号的列表,每次我点击这个按钮,我希望它转到一个新的引号。有人能解释一下这里出了什么问题吗?我该怎么解决呢?

代码语言:javascript
复制
<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>

我在互联网上找到了这个代码,当我使用这个代码时,它每次点击就会改变文本。

代码语言:javascript
复制
        var currentValue = parseInt(textField.value);

        // Add one
       currentValue++;

        // Put it back with the new +1'd value
        textField.value = currentValue;
        var quotes = new Array();

我为数组使用的代码几乎相同,但它不会改变每次点击的文本。对于数组,我需要做什么特别的事情吗?救命!!

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2014-01-11 22:17:52

它不会更改它,因为您声明了处理程序中的数组和索引,所以每次单击时都会在索引1处得到引号。

代码语言:javascript
复制
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.";

function buttonClickHandler() {
    var textField = document.getElementById("textField");
    textField.value = [++nextQuote];
}
票数 0
EN

Stack Overflow用户

发布于 2014-01-11 22:17:32

因为每次调用nextQuote时,都会将其重新设置为0。

票数 0
EN

Stack Overflow用户

发布于 2014-01-11 22:19:33

每次调用处理程序时,您都要将nextQuote设置为0。

var nextQuote = 0;

试着这样做:

代码语言:javascript
复制
var quotes = new Array();
    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.";

var nextQuote = 0;
var textField = document.getElementById("textField"); 


function buttonClickHandler() {
    if(nextQuote < 9) {
        nextQuote++;
    } else {
        nextQuote = 0;
    }
    textField.value = quotes[nextQuote];
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21068560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档