簡體   English   中英

使用JQUERY文本功能將文本區域中的內容替換為DIV時,br標簽不起作用

[英]br tag is not working while replace the content from textarea to DIV using JQUERY text function

br標記不適用於DIV。 我從更新內容: textarea字段中收到消息。 該消息有兩行,如“ Rama”,下一行為“ Lingam”。 但結果是“拉瑪
Lingam”,由$("#editQuotesRowLabel_Q_013").text(replaceContent);

如何在DIV中換行?

這是我的代碼:

function updateQuotes(){    
    var weg_quotes = $("#weg_quotes_Q_013").val();  
    replaceContent = weg_quotes.replace("\n", "<br>", "g");         
    $("#editQuotesRowLabel_Q_013").text(replaceContent);
}
Update Content: <textarea placeholder="Update your content..." id="weg_quotes_Q_013" name="weg_quotes" cols="80" rows="3" class="appQuotesTextarea" maxlength="200" required=""></textarea>
<button onclick="updateQuotes()" id="Q_013" name="update-btn" class="appQuotesBtn">Update</button>

<div id="editQuotesRowLabel_Q_013" class="appQuotesLabel">
    This is content div. 
</div>

要使用jquery插入HTML內容,您需要使用.html()而不是.text() 因此,您的情況最終是:

 $("#editQuotesRowLabel_Q_013").html(replaceContent);
 //                              |_ use html() instead of text()

資源 :

只需使用.html()而不是.text()因為textarea內容還將包含
標簽和.html()也可以顯示html, .text()僅考慮文本-不會設置html標簽(
在div中標記),因此請使用.html()而不是.text()

 function updateQuotes(obj){ var weg_quotes = $("#weg_quotes_Q_013").val(); replaceContent = weg_quotes.replace("\\n", "<br>", "g"); $("#editQuotesRowLabel_Q_013").html(replaceContent); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> Update Content: <textarea placeholder="Update your content..." id="weg_quotes_Q_013" name="weg_quotes" cols="80" rows="3" class="appQuotesTextarea" maxlength="200" required=""></textarea> <button onclick="updateQuotes(this)" id="Q_013" name="update-btn" class="appQuotesBtn">Update</button> <div id="editQuotesRowLabel_Q_013" class="appQuotesLabel"> This is content div. </div> 

試試這個:

$("#editQuotesRowLabel_Q_013").html(replaceContent);

//嘗試使用html()而不是text()

這將允許您使用html標簽:)

我不確定replace()方法是什么,但是replace是

str.replace(pattern/string, function/replacement)

所以你的代碼應該是

replaceContent = weg_quotes.replace(/\n/g, "<br>");  

並且在將html插入div時應該使用.html()

$("#editQuotesRowLabel_Q_013").html(replaceContent);

試試這個.html()而不是.text()

$("#editQuotesRowLabel_Q_013").html(replaceContent);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM