[英]Replace selected text inside p tag
我正在嘗試替換p標記中的選定文本。我已經處理了換行情況,但是由於某些原因,選定的文本仍然沒有被替換。這是html代碼。
<p id="1-pagedata">
(d) 3 sdsdsd random: Subject to the classes of this random retxxt wee than dfdf month day hello the tyuo dsds in twenty, the itol ghot qwerty ttqqo
</p>
這是JavaScript代碼。
function SelectText() {
var val = window.getSelection().toString();
alert(val);
$('#' + "1-pagedata").html($('#' + "1-pagedata").text().replace(/\r?\n|\r/g,""));
$('#' + "1-pagedata").html($('#' + "1-pagedata").text().replace(/[^\x20-\x7E]/gmi, ""));
$('#' + "1-pagedata").html($('#' + "1-pagedata").text().replace(val,"textbefore" + val + "textAfter"));
}
$(function() {
$('#hello').click(function() {
SelectText();
});
});
我還創建了一個jsfiddle代碼。 https://jsfiddle.net/zeeshidar/w50rwasm/有什么想法嗎?
您可以簡單地執行$("#1-pagedata").html('New text here');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.