简体   繁体   English

将换行符放在JS / jQuery中的变量数组中的文本字符串中

[英]Put line break in text string within a variable array in JS/jQuery

Noob Question: Noob问题:

I have a simple switch for some static text that is set to switch at a certain interval and for some reason I cannot get the line break to work within the string. 我有一个简单的开关用于某些静态文本设置为以特定间隔切换,并且由于某种原因我不能让换行符在字符串中工作。 I've tried a few different methods of using \\n or 我尝试了几种使用\\ n或者的方法
and even tried encoding and it won't break the string into two sentences. 甚至尝试编码,它不会将字符串分成两个句子。 Anyone see what I'm doing wrong? 有谁看到我做错了什么? The text change is working. 文本更改正在进行中。 I am trying to get the string to break into a new line where the "\\n" is located. 我试图让字符串进入“\\ n”所在的新行。

Here's a working fiddle: 这是一个工作小提琴:

JS Fiddle JS小提琴

Script: 脚本:

   var texts = ["Blah Blah"+"\n"+"Blah Blah Line two", "Over 15,000 cups of"+"\n"+"Starbucks drank daily!", "Another awesome%0D%0Aslogan here"];
var count = 0;
function changeText() {
    $("#example").text(texts[count]);
    count < 3 ? count++ : count = 0;
}
setInterval(changeText, 500);

Html: HTML:

<div id="Div-A"><h2><span id="example">Over 15,000 eligible<br/>dealers nationwide</span></h2></div>

You are using .text() to input your slogans. 您正在使用.text()输入您的标语。

I changed your .text() to .html() and just put simple line breaks in your sentences: 我将.text()更改为.html()并在句子中添加简单的换行符:

$(document).ready(function() {
    var texts = ["Over 5,000 beers <br /> drank today", "Over 15,000 cups of <br /> Starbucks drank daily!", "Another awesome<br />slogan here"];
    var count = 0;
    function changeText() {
        $("#example").html(texts[count]);
        count < 3 ? count++ : count = 0;
    }
    setInterval(changeText, 500);
});

http://jsfiddle.net/2dBVM/2/ http://jsfiddle.net/2dBVM/2/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM