每隔几秒更改一次的输入占位符
Input placeholder that changes every few seconds
<input id="home_search" type="text" placeholder="Kyle">
我正在寻找一种解决方案,允许占位符每 2 秒更改为预定的其他占位符。比如"John""Sally"
使用这段代码它工作得很好
$(document).ready(function(){
var placeHolder = ['one','two','three','four','five'];
var n=0;
var loopLength=placeHolder.length;
setInterval(function(){
if(n<loopLength){
var newPlaceholder = placeHolder[n];
n++;
$('input').attr('placeholder',newPlaceholder);
} else {
$('input').attr('placeholder',placeHolder[0]);
n=0;
}
},2000);
});
With the above code you can add as much values you want to the array it will work infinite and will return back to first placeholder when loop finish
更新工作 fiddle:https://jsfiddle.net/5pwuqkbp/2/
你是这个意思?
<script>
jQuery(document).ready(function () {
var i = 1;
setInterval(function () {
switch (i) {
case 1:
$("#home_search").attr('placeholder', 'John');
break;
case 2:
$("#home_search").attr('placeholder', 'Sally');
break;
}
i++;
if (i > 2) {
i = 1;
}
}, 2000);
});
</script>
<input id="home_search" type="text" placeholder="Kyle">
我正在寻找一种解决方案,允许占位符每 2 秒更改为预定的其他占位符。比如"John""Sally"
使用这段代码它工作得很好
$(document).ready(function(){
var placeHolder = ['one','two','three','four','five'];
var n=0;
var loopLength=placeHolder.length;
setInterval(function(){
if(n<loopLength){
var newPlaceholder = placeHolder[n];
n++;
$('input').attr('placeholder',newPlaceholder);
} else {
$('input').attr('placeholder',placeHolder[0]);
n=0;
}
},2000);
});
With the above code you can add as much values you want to the array it will work infinite and will return back to first placeholder when loop finish
更新工作 fiddle:https://jsfiddle.net/5pwuqkbp/2/
你是这个意思?
<script>
jQuery(document).ready(function () {
var i = 1;
setInterval(function () {
switch (i) {
case 1:
$("#home_search").attr('placeholder', 'John');
break;
case 2:
$("#home_search").attr('placeholder', 'Sally');
break;
}
i++;
if (i > 2) {
i = 1;
}
}, 2000);
});
</script>