我正在重构一些旧的JavaScript代码,并且正在进行大量的DOM操作.
var d = document;
var odv = d.createElement("div");
odv.style.display = "none";
this.OuterDiv = odv;
var t = d.createElement("table");
t.cellSpacing = 0;
t.className = "text";
odv.appendChild(t);
我想知道是否有更好的方法来使用jQuery.我一直在尝试:
var odv = $.create("div");
$.append(odv);
// And many more
但我不确定这是否更好.
我有以下代码:
var stringDisplay = "Hello\nWorld";
$("#meaning").text(stringDisplay);
它显示\n而不是换行符.
输出显示为Hello\nWorld.
我<br>也用tag来代替\n,但它仍然没有用.
我想使用引导程序将一长段字符串显示到表格单元格中。它包含一些换行符\r\n。style="white-space:pre"我尝试按照stackoverflow 帖子的建议使用,但这会破坏引导程序文本换行。我也尝试过更改,\r\n但<br>没有成功。<br>显示为文本。
这就是我的桌子的样子..
<table id="table1" class="table table-bordered">
    <tbody>
        <tr>
            <th class="col-md-2" scope="row">Description</th>
            <td id="description">Very long test with \r\n so I want to show this in multiline and also want to wrap the text using bootstrap.</td>
        </tr>
    </tbody>
</table>
我是JSON格式的新手,请原谅我缺乏知识.但不知何故,我需要在我的json_encode数组中添加一些换行符,以便它将使用多行进行解析.我搜索和搜索过,但没有任何东西适合我的情况.
它输出为:
Following formats accepted: www.website.com website.com website.net/something
但是我想输出这样的数据:
Website needs to be in the following formats:  
www.website.com  
website.com  
website.net/something
我试过了:
echo json_encode( 
    array( 'status' => 'failed',
           'message' => "Website needs to be in the following formats:\n
            www.website.com\n
            website.com\n
            website.net/something"
    ), JSON_PRETTY_PRINT);
但Javascript正在将其解析为文字字符串,因此新行被忽略并输出.我可以使用直接JSON格式将数据从PHP发送到javascript,还是必须使用数组?
我也试过用<br />.
编辑:
 
我输出以下方式:  
$.ajax({
    url: "/forms/content_process.php",
    type:'POST',
    data: $(".content_cat").serialize(),
    processData: false,
    dataType: "json",
    success: function(response) {
        if (response.status == "failed") {
            $(".content_status").css( "background-color", "red" );
            $(".content_status").text(response.message).slideDown("normal");
        } else {
            $(".content_status").css( "background-color", "green" );
            $(".content_status").text("Your …我已使用附加<div>到我的页面正文
$('body').append('<div id="itemInfo"><h2>TEST</h2></div>');
这<div>隐藏在我的CSS中.然后我<div>使用函数改变文本,如下所示.
    function appendMessage(messagestring) {
    $('#itemInfo h2').text(messagestring);
    $('#itemInfo').fadeIn();
    $('#itemInfo').fadeOut(3000);   
}
这是使用以下声明.
appendMessage("Something. ");
问题是,我似乎无法获得<br />或/n字符在消息中工作.如何在.text()函数中使用换行符?