新行未在JavaScript警报框中工作

时间:2018-03-15 08:24:49

标签: javascript jquery alert

HTML:

<p id="value">
 Challange Accepted.\nTested Ok
</p>

JS:

var result = jQuery('#value').text();
var response = String(result);
alert(response);

我的小提琴:https://jsfiddle.net/prg5h1zy/15/#&togetherjs=Rx9GCd4GAs

1 个答案:

答案 0 :(得分:2)

您需要将\n文字替换为行为类似新行的\n

&#13;
&#13;
var result = jQuery('#value').text();
var response = result.replace(/\\n/g,"\n");
alert(response);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p id="value">
 Challange Accepted.\nTested Ok
</p>
&#13;
&#13;
&#13;