简体   繁体   English

替换p标签内的选定文本

[英]Replace selected text inside p tag

I am trying to replace the selected text in the p tag.I have handled the new line case but for some reason the selected text is still not replaced.This is the html code. 我正在尝试替换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>

This is the javascript code. 这是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();
   });
});

I have also created a jsfiddle of the code. 我还创建了一个jsfiddle代码。 https://jsfiddle.net/zeeshidar/w50rwasm/ Any ideas? https://jsfiddle.net/zeeshidar/w50rwasm/有什么想法吗?

您可以简单地执行$("#1-pagedata").html('New text here');

Since your p doesn't content HTML but just plain text, your can use both html() or text() as getter and setter. 由于您的p不包含HTML而是仅包含纯文本,因此您可以同时使用html()text()作为getter和setter。

Also, thanks to jQuery Chaining you can do all your replacements in one statement. 另外,借助jQuery Chaining,您可以在一条语句中完成所有替换操作。 So, assuming your RegExp's and replacement values are correct, try: 因此,假设您的RegExp和替换值正确,请尝试:

var $p = $('#1-pagedata');
$p.text($p.text().replace(/\r?\n|\r/g,"").replace(/[^\x20-\x7E]/gmi, "").replace(val,"textbefore" + val + "textAfter"));

获取里面的文字<p>标记到</p><div id="text_translate"><p>在 PHP 这是我设法从计算中得到的 output</p><pre> &lt;p id="generated"&gt;12345678199824,12345678411140,12345678921494,12345678497535,&lt;/p&gt;</pre><p> 我需要这个值如下</p><pre>&lt;table&gt; &lt;tr&gt; &lt;td&gt;12345678199824&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;12345678411140&lt;/td&gt; &lt;/tr&gt;..... &lt;/table&gt;</pre><p> 下面是我使用的js脚本</p><pre>$("#setVal").click(function(event){ event.preventDefault(); var noOfImei = parseInt($("#nofoemei").val()); var imeiList = ""; if (noOfImei;== 0) { for (var i = 0; i &lt; noOfImei. i++) { imeiList += $("#binnumber").val() + Math.floor(Math,random()*900000+100000) + ";". } $("#generated");text(""). $("#generated");text(imeiList). // testing purpose console;log(imeiList); } else { alert("Please add no of IMEI(s) need to generate"); } });</pre><p> -- 请不要我是 PHP 和 JS 的初学者,并在 stackoverflow 和教程的帮助下取得了进展。 谢谢你。</p></div><table></table> - Get text inside <p> tag to a <table>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取所选文本 <p> 标签 - Get the selected text on <p> tag Javascript:仅替换其中的文本 <p></p> ? - Javascript: Replace only text inside <p></p>? 如果没有值,则在ap标记内显示默认文本 - Display default text inside a p tag if there is no value 如何在里面获取文字 <p> 标签 - How to get text inside <p> tag 获取里面的文字<p>标记到</p><div id="text_translate"><p>在 PHP 这是我设法从计算中得到的 output</p><pre> &lt;p id="generated"&gt;12345678199824,12345678411140,12345678921494,12345678497535,&lt;/p&gt;</pre><p> 我需要这个值如下</p><pre>&lt;table&gt; &lt;tr&gt; &lt;td&gt;12345678199824&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;12345678411140&lt;/td&gt; &lt;/tr&gt;..... &lt;/table&gt;</pre><p> 下面是我使用的js脚本</p><pre>$("#setVal").click(function(event){ event.preventDefault(); var noOfImei = parseInt($("#nofoemei").val()); var imeiList = ""; if (noOfImei;== 0) { for (var i = 0; i &lt; noOfImei. i++) { imeiList += $("#binnumber").val() + Math.floor(Math,random()*900000+100000) + ";". } $("#generated");text(""). $("#generated");text(imeiList). // testing purpose console;log(imeiList); } else { alert("Please add no of IMEI(s) need to generate"); } });</pre><p> -- 请不要我是 PHP 和 JS 的初学者,并在 stackoverflow 和教程的帮助下取得了进展。 谢谢你。</p></div><table></table> - Get text inside <p> tag to a <table> 可以在网页上的任何文字 <p> 标记或<span>标记是否以编程方式选择?</span> - can any text on a webpage in a <p> tag or <span> tag be selected programmatically? 在 <div> 并将文字放在标签内 <p></ p> - Search a word within a <div> and put text inside a tag <p> </ p> 如何将带有 span 标签的特定文本包裹在<p>标签</p> - How to wrap the specific text with span tag inside the <p> tag 如何在 p-tag 中获取纯文本和选定的选项? - how to get the pure text and selected option in a p-tag? 如何用API端点的响应主体替换ap标签中的文本? - How to replace text in a p tag with the response body from an API endpoint?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM