Dyl*_*ley -3 javascript jquery
我试图这样做,当我点击一个按钮时,文本将会改变.我已经能够实现这一点,但是,我想列出一个文本列表.对于每个按钮单击,文本应更改为新的.
我只能改变它一次.我不知道怎么回事.这是我的代码:
$(document).ready(function(){
$(".button").click(function(){
$("#text").text("I have changed!");
});
});
Run Code Online (Sandbox Code Playgroud)
你需要一个current计数器和一个数组texts.
片段
$(document).ready(function() {
// Step 1: Add the texts in an array.
var texts = ["Hello, World!", "Hello, Praveen!", "Hello, StackOverflow"];
// Step 2: Initialize a count.
var count = 0;
// Step 3: Programmatically add the first value.
$("#text").text(texts[count]);
$(".button").click(function() {
// Step 4: Increment the count.
count++;
// Step 5: Update the text.
// Step 6: Make sure you don't run of boundaries. So use modulus.
$("#text").text(texts[count % texts.length]);
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="text"></div>
<button class="button">Change</button>Run Code Online (Sandbox Code Playgroud)