2017-11-14 23 views
1
<center><p id="demo" style="color:black;"></p></center> 

<script> 

window.onload = function() { 
    typeWriter(); 
}; 
var i =0; 
var txt = "Would it be ok if I wrote you a rhyme? would it be ok if I opened my heart? Would it be ok if I took on the part Of being your man and showed you a view, One that only a real man could do? Would it be ok if I could make you smile? Would it be alright to look in your eyes? Would it be alright to never tell lies? Would it be alright to find a way? Would it be alright to long for the day To pull you close and whisper in your ear And tell you our feelings are nothing to fear? Would it be ok if I took some of your time? Would it be ok if I wrote you a rhyme? To tell you there is nothing I would rather do Than spend my whole life loving only you... "; 
var txt1= ''; 
var speed = 100; 
function typeWriter() { 
    if (i < txt.length) { 
    document.getElementById("demo").innerHTML += txt.charAt(i); 
document.getElementById("demo").innerHTML += txt1.charAt(i); 

    i++; 
     setTimeout(typeWriter, speed); 

    } 

} 
</script> 

私は次の行のすべての文章を詩として印刷したいと思っています...!しかし、私はそれをすることはできません助けて喜んで誰もがある場合はください!私は彼/彼女を感謝します おかげでアドバンス私は/ n、/ r/nを試してみましたが、失敗しました。私は試しましたが、失敗しました。

+1

私はあなたが(String.prototype.splitを見てみましょう示唆D)https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split – Azadrum

+0

また、 '
'タグを使用して、異なる行のテキストを区切ることができます。 – MysterX

+0

あなたは私に何を言っているのか正確なコードを教えてくれますか? –

答えて

1

たとえば、char "#"のような独自のフラグを使用してください。 インデックスでcharを単純にチェックし、タグbr /を使用します。 ソリューションの改善char? "新しい行のために。コードスニペットexample2を参照してください。

<center><p id="demo" style="color:black;"></p></center> 
 

 
<script> 
 

 
window.onload = function() { 
 
    typeWriter(); 
 
}; 
 
var i =0; 
 
var txt = "Would it be ok if I wrote you a rhyme?# would it be ok if I opened my heart? # Would it be ok if I took on the part Of being your man and showed you a view, One that only a real man could do?# Would it be ok if I could make you smile?# Would it be alright to look in your eyes? Would it be alright to never tell lies?# Would it be alright to find a way?# Would it be alright to long for the day To pull you close and whisper in your ear And tell you our feelings are nothing to fear? Would it be ok if I took some of your time?# Would it be ok if I wrote you a rhyme?# To tell you there is nothing I would rather do Than spend my whole life loving only you... "; 
 
var txt1= ''; 
 
var speed = 100; 
 
function typeWriter() { 
 
    if (i < txt.length) { 
 
    
 
    if (txt.charAt(i) == "#"){ 
 
    document.getElementById("demo").innerHTML += "<br/>"; 
 
    } 
 
    else { 
 
    document.getElementById("demo").innerHTML += txt.charAt(i); 
 
    } 
 
    document.getElementById("demo").innerHTML += txt1.charAt(i); 
 

 
    i++; 
 
     setTimeout(typeWriter, speed); 
 

 
    } 
 

 
} 
 
</script>

例2:太字(少しパーサ)と

<center><p id="demo" style="color:black;"></p></center> 
 

 
<script> 
 

 
window.onload = function() { 
 
    typeWriter(); 
 
}; 
 
var i =0; 
 
var txt = "Would it be ok if I wrote you a rhyme? would it be ok if I opened my heart? Would it be ok if I took on the part Of being your man and showed you a view, One that only a real man could do? Would it be ok if I could make you smile? Would it be alright to look in your eyes? Would it be alright to never tell lies?Would it be alright to find a way? Would it be alright to long for the day To pull you close and whisper in your ear And tell you our feelings are nothing to fear? Would it be ok if I took some of your time? Would it be ok if I wrote you a rhyme? To tell you there is nothing I would rather do Than spend my whole life loving only you... "; 
 
var txt1= ''; 
 
var speed = 100; 
 
function typeWriter() { 
 
    if (i < txt.length) { 
 
    
 
    if (txt.charAt(i) == "?"){ 
 
    
 
    document.getElementById("demo").innerHTML += txt.charAt(i); 
 
    document.getElementById("demo").innerHTML += "<br/>"; 
 
    
 
    } 
 
    else { 
 
    document.getElementById("demo").innerHTML += txt.charAt(i); 
 
    } 
 
    document.getElementById("demo").innerHTML += txt1.charAt(i); 
 

 
    i++; 
 
     setTimeout(typeWriter, speed); 
 

 
    } 
 

 
} 
 
</script>

例: それは少し複雑です。 1つのFLAGも追加メソッドを持つ動的な作成要素が必要です。

<center><p id="demo" style="color:black;"></p></center> 
 

 
<script> 
 

 
window.onload = function() { 
 
    typeWriter(); 
 
}; 
 
var i =0; 
 
var txt = "Would it be ok if I wrote you a #rhyme#? would it be ok if I opened my #heart#? Would it be ok if I took on the part Of being your man and showed you a view, One that only a real man could do? Would it be ok if I could make you smile? Would it be alright to look in your eyes? Would it be alright to never tell lies?Would it be alright to find a way? Would it be alright to long for the day To pull you close and whisper in your ear And tell you our feelings are nothing to fear? Would it be ok if I took some of your time? Would it be ok if I wrote you a rhyme? To tell you there is nothing I would rather do Than spend my whole life loving only you... "; 
 
var txt1= ''; 
 
var speed = 100; 
 

 
var BOLD_ELE = null; 
 
var isBold = false; 
 

 
function typeWriter() { 
 
    if (i < txt.length) { 
 
    
 
    if (isBold == true){ 
 
     
 
    var BOLD_ELE = document.createElement('B'); 
 
    document.getElementById("demo").appendChild(BOLD_ELE); 
 
     
 
    } 
 
    
 
    if (txt.charAt(i) == "?"){ 
 
    
 
    document.getElementById("demo").innerHTML += txt.charAt(i); 
 
    document.getElementById("demo").innerHTML += "<br/>"; 
 
    
 
    } 
 
    else if (txt.charAt(i) == "#"){ 
 
     if (isBold == false) { 
 
     isBold = true 
 
     } 
 
     else { 
 
     isBold = false 
 
     } 
 
    
 
    } 
 
    else { 
 
    
 
    if (isBold == false){ 
 
    document.getElementById("demo").innerHTML += txt.charAt(i); 
 
    } 
 
    else { 
 
    BOLD_ELE.innerHTML += txt.charAt(i); 
 
    } 
 
    
 
    } 
 
    
 
    document.getElementById("demo").innerHTML += txt1.charAt(i); 
 

 
    i++; 
 
     setTimeout(typeWriter, speed); 
 

 
    } 
 

 
} 
 
</script>

+0

どのように私はそれのテキストを大胆にしますか? ありがとうございました –

+0

答えを受け入れることを知っていますか? –

関連する問題