我有這段代碼片段。顯示延遲字母輸入字段中的文字
<div class="content__img-txt">
<a class="input-group">
<input class="input-group-field" type="text">
<div class="input-group-button">
<input type="submit" class="button" value="»">
</div>
</div>
function showLetter (field, text, delay) {
$(field).val(text.substring(0,1));
for(var i = 2; i <= text.length; i++)
{
setTimeout(function(){
$(field).val(text.substring(0,i));
}, delay);
}
}
showLetter (".input-group-field", "Show letter for letter with a delay of 1 second", 1000);
The console says : Uncaught TypeError: Cannot read property 'length' of undefined.
如何顯示在輸入字段,但信字母文字,而不是整個文本同時顯示,該文件完全加載後?
你是否介意我們一些HTML標記? – Aer0
在我的Chrome上運行良好。但文字一起顯示。 – yaochiqkl
您確定控制檯消息指向該代碼嗎?你的主要問題似乎是你的文本會一起顯示,因爲當你的超時執行'i'將會是字符串的長度,參見[JavaScript閉包內部循環 - 簡單的實例](http:// stackoverflow .com/questions/750486/javascript-closure-inside-loops-simple-practical-example) –