Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何替换<;br/>;在jquery中使用\n(新行)标记_Javascript_Jquery_Jsp Tags - Fatal编程技术网

Javascript 如何替换<;br/>;在jquery中使用\n(新行)标记

Javascript 如何替换<;br/>;在jquery中使用\n(新行)标记,javascript,jquery,jsp-tags,Javascript,Jquery,Jsp Tags,我使用这些代码片段将jquery中的标记替换为“\n”(新行)。但它不起作用 $("#UserTextArea").html(data.projectDescription).replace(/\<br\>/g, "\n"); $(“#UserTextArea”).html(data.projectDescription)。替换(/\/g,“\n”); 但它不起作用,为什么会这样?$(“#UserTextArea”).html(data.projectDescription.

我使用这些代码片段将jquery中的

标记替换为“\n”(新行)。但它不起作用

  $("#UserTextArea").html(data.projectDescription).replace(/\<br\>/g, "\n");
$(“#UserTextArea”).html(data.projectDescription)。替换(/\/g,“\n”);
但它不起作用,为什么会这样?

$(“#UserTextArea”).html(data.projectDescription.replace(/\/g,“\n”)
$(“#UserTextArea”).html(data.projectDescription.replace(/\/g,“\n”)

.replace()
不会更改内容本身。它返回一个新字符串,因此如果要使用该新字符串,必须将
.replace()
中的返回值放在某个位置

如果您的目标是
,则可能还需要通过转义\并将其设置为可选来修复正则表达式

仅供参考,在HTML中,如果您只是试图删除
#UserTextArea
中的所有

标记,则a
\n
不会执行任何操作,您可以使用DOM解析器执行以下操作:

$("#UserTextArea br").remove();
或者,如果您只想将带有

标记的字符串替换为可用于其他内容的变量,可以执行以下操作:

var str = $("#UserTextArea").html().replace(/\<br\\?>/g, "\n");
$("#UserTextArea").html(data.projectDescription..replace(/\<br\\?>/g, "\n"));
.replace()
不会更改内容本身。它返回一个新字符串,因此如果要使用该新字符串,必须将
.replace()
中的返回值放在某个位置

如果您的目标是
,则可能还需要通过转义\并将其设置为可选来修复正则表达式

仅供参考,在HTML中,如果您只是试图删除
#UserTextArea
中的所有

标记,则a
\n
不会执行任何操作,您可以使用DOM解析器执行以下操作:

$("#UserTextArea br").remove();
或者,如果您只想将带有

标记的字符串替换为可用于其他内容的变量,可以执行以下操作:

var str = $("#UserTextArea").html().replace(/\<br\\?>/g, "\n");
$("#UserTextArea").html(data.projectDescription..replace(/\<br\\?>/g, "\n"));
$('UserTextArea').html(data.projectDescription.replace(/\/g,'\n'));
或者,要搜索并替换元素的内部HTML:

var $element = $('#UserTextArea');
var html = $element.html();
$element.html(html.replace(/\<br\s*\>/g, '\n'));
var$element=$('#UserTextArea');
var html=$element.html();
$element.html(html.replace(/\/g,'\n'));
$('#UserTextArea').html(data.projectDescription.replace(/\/g,'\n'));
或者,要搜索并替换元素的内部HTML:

var $element = $('#UserTextArea');
var html = $element.html();
$element.html(html.replace(/\<br\s*\>/g, '\n'));
var$element=$('#UserTextArea');
var html=$element.html();
$element.html(html.replace(/\/g,'\n'));

它不是工作伙伴。我还使用$(“#UserTextArea”).html(data.projectDescription.replace(“
,”\n”);它也不工作。
数据的内容是什么。projectDescription
?它不工作。我还使用$(“#UserTextArea”).html(data.projectDescription.replace(“
,”\n”);它也不起作用
数据的内容是什么。projectDescription
?我认为换行符确实意味着pre标记内的内容?我认为换行符确实意味着pre标记内的内容?