我正在寻找一个简单的脚本,在所有跨度上使用class =值旋转1秒间隔的货币。
<script type="text/javascript">
$(document).ready(function () {
var str1 = "£";
var str2 = "€";
var str3 = "$";
$("span.value").text(str1); //how can I rotate between the str1, 2 and 3 with a 1 second interval?
});
</script>
HTML看起来像这样:
<h2>Get <span class="value"></span> discount.</h2>
<h3>Get <span class="value"></span> extra.</h3>
依旧......
感谢所有帮助!
答案 0 :(得分:2)
以下内容有望满足:
$(document).ready(function () {
var currencyIndex = 0;
var currencies = ['£', '€', '$'];
var cInterval = setInterval(function() {
$("span.value").text(function() {
return currencies[currencyIndex];
});
currencyIndex = (currencyIndex + 1) % currencies.length;
}, 1000);
});
请记住在HTML中使用实体,而不是符号,否则会得到奇怪的结果。
答案 1 :(得分:1)
您可以使用setInterval功能:
var currencies = ['£', '€', '$'];
var index = 0;
window.setInterval(function() {
var value = currencies[(index++) % currencies.length];
$('span.value').text(value);
}, 1000);
答案 2 :(得分:0)
我建议把它们变成一个数组:
var i;
window.setInterval(function() {
$('span.value').text(currencies[(++i % currencies.length) + 1]);
},1000)