【发布时间】:2018-03-02 00:30:57
【问题描述】:
我尝试编写一个点击函数来在 jquery 的点击函数上显示下一个数组项,但它不起作用。请指教:)
var array = [one, two, three, four, five];
$('#countButton').click(function(){
for(var i = 0; i < array.length; i++){
$('#displayCount').html(array[i++]);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="button" value="Count" id="countButton" />
<p>The button was pressed <span id="displayCount">0</span> times.</p>
【问题讨论】:
-
不要循环。将您的计数器存储在点击事件之外,并在设置 html 后增加它