[英]replace with some other text
我陷入了需要替換
與其他一些文字。
我嘗試了以下代碼。
var text = $("#nbspData").text().replace(' ','a'); $("#removedData").html(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="nbspData">this is me.</p> <p id="removedData"></p>
但是,當我檢查代碼時
未刪除。
發生這種情況是因為text()
不返回那些編碼的字符,而只是返回您看到的文本,例如:“這就是我”。 因此,沒有什么可替代的。 將text()
更改為html()
:
var text = $("#nbspData").html().replace(' ','a');
var text = $("#nbspData").html().replace(' ','a'); $("#removedData").html(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="nbspData">this is me.</p> <p id="removedData"></p>
其他信息:如果要替換所有出現的
請使用正則表達式替換
:
var text = $("#nbspData").html().replace(/\\ /g,'a'); console.log(text);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <p id="nbspData">this is me.</p>
這是HTML標記和&nbsp等的解決方案,您可以根據自己的需求刪除和添加條件
convertHtmlToText(passHtmlBlock)
{
str = str.toString();
return str.replace(/<[^>]*(>|$)| |‌|»|«|>/g, 'replaceWithAnyText');
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.