javascript - 文字不出现在按钮上

标签 javascript html button

我正在尝试创建四个按钮,其中四个标签以随机顺序出现:

<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice0"></button>
</div>
<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice1"></button>
</div>
<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice2"></button>
</div>
<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice3"></button>
</div>

标签存放在

var choiceLabels = ["Hola", "Hole", "Holo", "Holu"];

然后代码遍历选项以将它们粘贴到按钮中:

for (var i=0; i<choiceLabels; i++) {
    var theID="#quizChoice"+i.toString();
    $(theID).text(choiceLabels[i])
};

但文本不会出现在按钮上 - 它们保持空白。 有人知道为什么吗?

最佳答案

您的错误在这里:i<choiceLabels . choiceLabels是一个数组,所以你必须做 i<choiceLabels.length .

var choiceLabels = ["Hola", "Hole", "Holo", "Holu"];
for (var i=0; i<choiceLabels.length; i++) {
    var theID="#quizChoice"+i.toString();
    $(theID).text(choiceLabels[i])
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice0"></button>
</div>
<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice1"></button>
</div>
<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice2"></button>
</div>
<div class="row text-center center-block">
                <button class="quizChoice btn btn-md btn-info" id="quizChoice3"></button>
</div>

关于javascript - 文字不出现在按钮上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49234393/

相关文章:

html - 在 REACT 中使用 CSS

javascript - 从 jQuery + AJAX 请求中选择时,IE8 返回 NULL

javascript - 在 redux-orm 模式实例上使用 reducer 方法

javascript - 在没有任何html标签的html中隐藏文本

jquery - 传递带有参数的函数以在 jQuery 中悬停

java - 显示隐藏的html代码的按钮?

javascript - Backbone.js,为什么这个回调被多次触发?

javascript - 链接不会重定向到其他页面,但在定向到同一页面上的某个部分时有效

java - 在 Java 中使用自定义字体创建按钮会导致按钮形状奇怪

java - 如何在不同的 Activity 中调用/调用重复按钮的类