Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Jquery-在字符串中强制换行时遇到问题_Javascript_Jquery_Html - Fatal编程技术网

Javascript Jquery-在字符串中强制换行时遇到问题

Javascript Jquery-在字符串中强制换行时遇到问题,javascript,jquery,html,Javascript,Jquery,Html,我有一个字符串,我正在向屏幕上一次写入一个字符。字符串应该打印在三行上,我尝试使用换行符来打印,但整个字符串仍然显示在一行上。我做错了什么 <?php include("header.php"); ?> <body> <div id="content" class="termFont" align="left"> <p> <p class="caption"></p><p class="cursor">|<

我有一个字符串,我正在向屏幕上一次写入一个字符。字符串应该打印在三行上,我尝试使用换行符来打印,但整个字符串仍然显示在一行上。我做错了什么

<?php
include("header.php");
?>

<body>
<div id="content" class="termFont" align="left">
<p>
<p class="caption"></p><p class="cursor">|</p>

</p>
</div>
<script type="text/javascript">
var captionLength = 0;
var caption = "Greetings Professor Falken\n\nA strange game. The only winning move is not to play.\n\nHow about a nice game of chess?";


function cursorAnimation()
{
  $("p.cursor").animate(
  {
    opacity: 0
  }, "fast", "swing").animate(
  {
    opacity: 1
  }, "fast", "swing");
}

$(document).ready(function()
{
    blinkingCursor();
 setInterval ( "cursorAnimation()", 600 );
});

function blinkingCursor()
{
  type();
}

function type()
{
  $('p.caption').html(caption.substr(0, captionLength++));
  if(captionLength < caption.length+1)
  {
    setTimeout("type()", 50);
  }
  else
  {
    captionLength = 0;
    caption = "";
  }
}
</script>
</body>
</html>


|

var captionLength=0; var caption=“你好,法尔肯教授\n\n一场奇怪的游戏。唯一获胜的一步是不下棋。\n\n下一盘好棋怎么样?”; 函数游标动画() { $(“p.cursor”).animate( { 不透明度:0 }“快速”、“摆动”)。制作动画( { 不透明度:1 }","快速","摇摆";; } $(文档).ready(函数() { 闪烁光标(); setInterval(“cursorAnimation()”,600); }); 函数闪烁光标() { 类型(); } 函数类型() { $('p.caption').html(caption.substr(0,captionLength++); if(标题长度<标题长度+1) { setTimeout(“type()”,50); } 其他的 { captionLength=0; 标题=”; } }
在字符串中添加一个

,而不是“\n”。HTML通常不关心空白(除非样式如此)

另外,当您调用
setTimeout()
时,请执行以下操作:

setTimeout(type, 50);
使用字符串可能会导致各种奇怪的问题,而且几乎没有必要这样做

编辑-至于我对HTML样式的评论,您可能会在CSS中添加“空白:pre”作为“caption”元素。然后你的换行符就会被遵守