JavaScript - 如何逐个字符地显示文本

时间:2015-12-19 12:15:32

标签: javascript html arrays

我尝试使用JavaScript逐个字符地显示从数组中获取的文本。我已经成功完成了阵列的一部分。我无法找到换行线并显示其余部分的方法。

var container = document.getElementById("container");

var notes = [
{scenario: 1, intro: "This is the introduction.", que: "What is the weight of ....?"},
{scenario: 2, intro: "This is the second scen.", que: "What is the second law of ...?"},
{scenario: 3, intro: "This is the third thing.", que: "What is the third law of ...?"},
];


function splTxt(txt) {
    // Split string into characters                                                                                                                                           
    t = txt.split('');
return t
}

function terminal(cl, i) {
// Create a div element and display message                                                                                                                               
var div = document.createElement('div');
div.className = cl;
container.appendChild(div);

// Take the first element of the array                                                                                                                                    
// and extract the intro string                                                                                                                                           
var txt = splTxt(notes[0].intro);
var i = 0;

// Display text, character by character                                                                                                                                   
var display = setInterval(function() {
    div.textContent += txt[i];
    if (i == (txt.length-1)) {
        clearInterval(display);
    }
    i += 1
}, 100);

}

terminal('blueTh', 0);

显示notes[0].intro后,我希望转到新行并显示notes[0].que

我试过

var txt = splTxt(notes[0].intro + '<br />' +  notes[0].que);

但显然,它只显示<br />并在同一行打印两条消息。

2 个答案:

答案 0 :(得分:2)

您有两种选择:

  • 插入<br />并告诉浏览器将其解析为HTML。

    您可以使用innerHTML属性代替textContent来执行此操作。

    这将允许您使用<br />之类的HTML内容,但是当它们应该是纯文本时,您必须转义&<>。如果您不信任该文本,请不要这样做。

    &#13;
    &#13;
    var container = document.getElementById("container");
    var notes = [
      {scenario: 1, intro: "This is the introduction.", que: "What is the weight of ....?"},
      {scenario: 2, intro: "This is the second scen.", que: "What is the second law of ...?"},
      {scenario: 3, intro: "This is the third thing.", que: "What is the third law of ...?"}
    ];
    function terminal(cl, i) {
      var div = document.createElement('div');
      div.className = cl;
      container.appendChild(div);
      var txt = [notes[0].intro, notes[0].que].join('\n').split('');
      var i = 0;
      (function display() {
        if(i < txt.length) {
          div.innerHTML += txt[i].replace('\n', '<br />');
          ++i;
          setTimeout(display, 100);
        }
      })();
    }
    terminal('blueTh', 0);
    &#13;
    <div id="container"></div>
    &#13;
    &#13;
    &#13;

  • 插入换行符并告诉浏览器正确显示它。

    在HTML中,空白字符默认情况下会折叠。您可以通过将white-space CSS属性设置为prepre-wrappre-line来更改此行为。例如,white-space: pre会保留所有空格,并且不会包装文本。

    &#13;
    &#13;
    var container = document.getElementById("container");
    var notes = [
      {scenario: 1, intro: "This is the introduction.", que: "What is the weight of ....?"},
      {scenario: 2, intro: "This is the second scen.", que: "What is the second law of ...?"},
      {scenario: 3, intro: "This is the third thing.", que: "What is the third law of ...?"}
    ];
    function terminal(cl, i) {
      var div = document.createElement('div');
      div.className = cl;
      container.appendChild(div);
      var txt = [notes[0].intro, notes[0].que].join('\n').split('');
      var i = 0;
      (function display() {
        if(i < txt.length) {
          div.textContent += txt[i];
          ++i;
          setTimeout(display, 100);
        }
      })();
    }
    terminal('blueTh', 0);
    &#13;
    #container {
      white-space: pre-line;
    }
    &#13;
    <div id="container"></div>
    &#13;
    &#13;
    &#13;

答案 1 :(得分:0)

insertMany
message = document.getElementById("fly").innerHTML; // $ = taking a new line
distance = 150; // pixel(s)
speed = 20; // milliseconds

var txt="",
	num=0,
	num4=0,
	flyofle="",
	flyofwi="",
	flyofto="",
	fly=document.getElementById("fly");


function stfly() {
	for(i=0;i != message.length;i++) {
		if(message.charAt(i) != "$")
			txt += "<span style='position:relative;visibility:hidden;' id='n"+i+"'>"+message.charAt(i)+"<\/span>";
		else
			txt += "<br>";
	}
	fly.innerHTML = txt;
	txt = "";
	flyofle = fly.offsetLeft;
	flyofwi = fly.offsetWidth;
	flyofto = fly.offsetTop;
	fly2b();
}

function fly2b() {
	if(num4 != message.length) {
		if(message.charAt(num4) != "$") {
			var then = document.getElementById("n" + num4);
			then.style.left = flyofle - then.offsetLeft + flyofwi / 2;
			then.style.top = flyofto - then.offsetTop + distance;
			fly3(then.id, parseInt(then.style.left), parseInt(then.style.left) / 5, parseInt(then.style.top), parseInt(then.style.top) / 5);
		}
		num4++;
		setTimeout("fly2b()", speed);
	}
}

function fly3(target,lef2,num2,top2,num3) {
	if((Math.floor(top2) != 0 && Math.floor(top2) != -1) || (Math.floor(lef2) != 0 && Math.floor(lef2) != -1)) {
		if(lef2 >= 0)
			lef2 -= num2;
		else
			lef2 += num2 * -1;
		if(Math.floor(lef2) != -1) {
			document.getElementById(target).style.visibility = "visible";
			document.getElementById(target).style.left = Math.floor(lef2);
		} else {
			document.getElementById(target).style.visibility = "visible";
			document.getElementById(target).style.left = Math.floor(lef2 + 1);
		}
		if(lef2 >= 0)
			top2 -= num3
		else
			top2 += num3 * -1;
		if(Math.floor(top2) != -1)
			document.getElementById(target).style.top = Math.floor(top2);
		else
			document.getElementById(target).style.top = Math.floor(top2 + 1);
		setTimeout("fly3('"+target+"',"+lef2+","+num2+","+top2+","+num3+")",25)
	}
}

stfly()