javascript 在文本后附加换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10793439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 11:03:24  来源:igfitidea点击:

Append line-break after text

javascriptjquery

提问by user1404304

I'm trying to append a line break to a line of text I'm loading from an xml page using the following code:

我正在尝试使用以下代码将换行符附加到我从 xml 页面加载的一行文本中:

$("#questionNumber").text(questionInARow).append('<br>');

The text is loading okay, but the append is being ignored. I've also tried this:

文本加载正常,但附加内容被忽略。我也试过这个:

$("#questionNumber").text(questionInARow).html("<br>");

and this is ignored likewise. Is my syntax wrong, or my method just bad?

这同样被忽略。是我的语法错误,还是我的方法不好?

Thank you in advance.

先感谢您。

采纳答案by Ashwin Krishnamurthy

Markup

标记

<div id='temp'></div>

JS

JS

$('#temp').text("hello").html($("#temp").html() + "<br/> how do you do? "); //This is one way
$('#temp').html("hello").append("<br/> how do you do?"); //Yet another way using the 'append' method.

Fiddle

小提琴

回答by Sampson

It's much simpler than you think:

它比你想象的要简单得多:

$("#questionNumber").html( questionInARow + "<br/>" );

Fiddle: http://jsfiddle.net/8jyqH/

小提琴:http: //jsfiddle.net/8jyqH/

回答by zhujy_8833

It seems that

看起来

<br/> 

is a tag. But you used .html() and .append() methods of jQuery, both of them are inserting contents into content node.

是一个标签。但是您使用了 jQuery 的 .html() 和 .append() 方法,它们都将内容插入到内容节点中。