我想将中心下一行中的每个句子打印为 Peom,我已经尝试了 /n ,/r/n,但我失败了

  • 本文关键字:Peom 失败 句子 一行 打印 javascript
  • 更新时间 :
  • 英文 :

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

我想把下一行的每一句话都打印成一首诗......!但是如果有人愿意帮助,我就做不到。!我会感谢他/她提前致谢

使用您自己的标志,例如字符"#"。我在索引处使用简单的字符检查并使用标签 br/。更好的解决方案使用字符"?"作为新行。请参阅代码片段示例 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 += "<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>

粗体(小解析器(的例子:这有点复杂.我们需要带有附加方法的动态创建元素也有一个 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>

最新更新