简体   繁体   English

使用JQUERY文本功能将文本区域中的内容替换为DIV时,br标签不起作用

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

The br tag is not working on DIV. br标记不适用于DIV。 I am getting the message from Update Content: textarea field. 我从更新内容: textarea字段中收到消息。 The message have the 2 lines like "Rama" next line "Lingam". 该消息有两行,如“ Rama”,下一行为“ Lingam”。 But the result is "Rama 但结果是“拉玛
Lingam" by $("#editQuotesRowLabel_Q_013").text(replaceContent); Lingam”,由$("#editQuotesRowLabel_Q_013").text(replaceContent);

How to make a new line in DIV? 如何在DIV中换行?

Here is my code: 这是我的代码:

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>

To insert HTML content using jquery you need to use .html() instead of .text() . 要使用jquery插入HTML内容,您需要使用.html()而不是.text() So in your case you end up with : 因此,您的情况最终是:

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

Source : 资源 :

Just use .html() instead of .text() because textarea content will also contain 只需使用.html()而不是.text()因为textarea内容还将包含
tags and .html() can show html also, .text() will only consider text - it will not set html tags ( 标签和.html()也可以显示html, .text()仅考虑文本-不会设置html标签(
tag here) in div, so use .html() instead of .text() 在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> 

Try with this: 试试这个:

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

// Try to use html() instead of text() //尝试使用html()而不是text()

This will allow you to use html tags :) 这将允许您使用html标签:)

I am not sure what that replace() method is, but it replace is 我不确定replace()方法是什么,但是replace是

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

So your code should be 所以你的代码应该是

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

and you should be using .html() when you are inserting html into a div. 并且在将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