所以我试图在我的文本框中显示键入的单词如下例所示:(thomas)将显示为=" t,th,tho,thom,thoma,thomas"。我在charAt中输入什么来实现这一目标?还是我需要添加别的东西?提前致谢!
<head>
<meta charset="utf-8">
<script>
function printit()
{
var temptext = document.getElementById("mytext").value;
var tempa = temptext.charAt();
document.getElementById("translated").innerHTML=tempa;
}
</script>
</head>
<body>
<h1> </h1>
<form name="f1">
<input type="text" id="mytext" value="" />
<input type="button" value="print" onclick="printit()"/>
</form>
<div id="translated" style="background-color:gray"></div>
</body>
</html>
答案 0 :(得分:1)
使用Array.from
和String#slice
方法的ES6解决方案。
<script>
function printit() {
var temptext = document.getElementById("mytext").value;
document.getElementById("translated").innerHTML = Array.from({
length: temptext.length
}, (v, i) => temptext.slice(0, i + 1)).join();
}
</script>
<form name="f1">
<input type="text" id="mytext" value="" />
<input type="button" value="print" onclick="printit()" />
</form>
<div id="translated" style="background-color:gray"></div>
&#13;
使用Array#map
和String#split
方法。
<script>
function printit() {
var temptext = document.getElementById("mytext").value;
document.getElementById("translated").innerHTML = temptext.split('').map(function(v, i) {
return temptext.slice(0, i + 1)
}).join();
}
</script>
<form name="f1">
<input type="text" id="mytext" value="" />
<input type="button" value="print" onclick="printit()" />
</form>
<div id="translated" style="background-color:gray"></div>
&#13;
答案 1 :(得分:1)
更简单的解决方案:
function writemore(){
var a = document.getElementById("mytext").value;
var out = [];
for(var i=1;i<a.length+1;i++){
out.push(a.substring(0,i));
}
document.getElementById("translated").innerHTML = out.join(',');
};
&#13;
<form name="f1">
<input type="text" id="mytext" value="" />
<input type="button" value="print" onclick="writemore();" />
</form>
<div id="translated" style="background-color:gray"></div>
&#13;
答案 2 :(得分:0)
您可以使用Array.prototype.map
和String.prototype.slice
来生成预期结果。
function printit(){
var text = document.getElementById('mytext').value;
document.getElementById("translated").innerHTML = text.split('').map(function(v, i){ return text.slice(0, i + 1)}).join(', ');
}
<h1> </h1>
<form name="f1">
<input type="text" id="mytext" value="" />
<input type="button" value="print" onclick="printit()" />
</form>
<div id="translated" style="background-color:gray"></div>
答案 3 :(得分:0)
If you want to display "t, th, tho, thom, thoma, thomas" you should to store your position of charAt(position+1) and make for condition.., so :
t : 0==> display = display + ", "+ charAt(position+1)
th : 1==> display = display + ", "+ charAt(position+1)
tho: 2 ==> display = display + ", "+ charAt(position+1)
答案 4 :(得分:0)
最简单的方法,如果您使用charAt进行设置,将是:
var outputString = "", inputString = "Thomas", i = -1, inputLength = inputString.length
while(++i < inputLength){
outputString += inputString.charAt(i)
console.log(outputString)
}
如果它真的需要打印,那么,th,thom,thoma,thomas&#34;而不是一系列日志,这个版本将完成
var outputString = "", outputArray = [], inputString = "Thomas", i = -1, inputLength = inputString.length
while(++i < inputLength){
outputString += inputString.charAt(i)
outputArray.push(outputString)
}
console.log(outputArray.join(", "))