2016-11-30 1 views
1

다음 예와 같이 내 텍스트 상자에 단어를 입력하려고합니다. (thomas) = ​​"t, th, tho, thom, thoma, thomas ". 그 일을하기 위해 내가 charAt에 무엇을 입력합니까? 또는 다른 것을 추가해야합니까? 미리 감사드립니다! Array.fromString#slice 및 방법을 사용 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> 
+1

[루프] (https : //developer.mozilla) 내의 ['substring'] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) .org/ko-ko/docs/Web/JavaScript/Reference/Statements/for)는 더 간단합니다. – Teemu

+0

@Teemu 참으로 :) – xShirase

답변

1

ES6 용액. Array#mapString#split 및 방법을 사용


<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>
.

1

간단한 해결책

<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>

:

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(','); 
 
};
<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>

0

당신은 예상 된 결과를 생성하는 Array.prototype.mapString.prototype.slice를 사용할 수 있습니다. 당신이 수 charAt을 사용하여 설정하는 경우

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>

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) 
0

간단한 방법이 될 것이다 :

var outputString = "", inputString = "Thomas", i = -1, inputLength = inputString.length 

while(++i < inputLength){ 
    outputString += inputString.charAt(i) 
    console.log(outputString) 
} 

말 그대로 "t, 일을 인쇄 할 필요가있는 경우, tho, thom, thoma, thomas "및 일련의 로그가 아닌이 버전은이를 수행합니다.

var outputString = "", outputArray = [], inputString = "Thomas", i = -1, inputLength = inputString.length 

while(++i < inputLength){ 
    outputString += inputString.charAt(i) 
    outputArray.push(outputString) 
} 


console.log(outputArray.join(", "))