试图添加/添加多个段落? [英] Trying to append/prepend multiple paragraphs?

查看:108
本文介绍了试图添加/添加多个段落?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在添加或添加带有段落之间自动放置的换行符的文本时遇到问题.例如:

I'm having a problem with appending or prepending text with automatically placed line returns in it between paragraphs. For example:

<script>
    $(document).ready(function () {
        $('#somelink').click(function () {
            $('#somediv').append("<p>Here's my first paragraph blah blah blah</p>
    <p>Here's my second paragraph blah blah blah blah</p>
    <p>Oh here's a third paragraph blah blah</p>");
        });
    });
</script>

似乎不起作用,因为段落之间的行返回.我发现<p>标记是否紧挨着就可以了,例如:

doesn't seem to work because of the line return between paragraphs. I found if the <p> tags are right up next to one another it's fine, for example:

<p>first paragraph</p><p>second paragraph</p>

而不是:

<p>first paragraph</p>
<p>second paragraph</p>

希望这是有道理的.问题是由这些附录放置的文本是用户生成的.如果我要更新文本内容,那很好,因为我知道消除输入的文本的行返回.但是,如果我的客户最终接管了更新并使用CMS的内置文本编辑器来添加新段落,它将自动创建行返回并中断脚本.基本上,文本编辑器会自动设置文本的格式,例如上面的无效示例.

Hopefully that makes sense. The issue is the text being placed by these appends is user generated. If I'm updating the text content it's fine, because I know to eliminate the line returns for the text I enter. However if my client ends up taking over updates and uses the CMS's built-in text editor to say add a new paragraph, it'll automatically create line returns and break the script. Basically the text editor automatically formats the text like the not working example above.

有什么办法可以让该函数删除那些换行符并使<p>标记彼此相邻运行?还是有一种方法可以保留带有标签的原样文本,而不会破坏脚本?

Is there any way I can have the function remove those line breaks and get the <p> tags to run right next to each other? Or is there a way to preserve the text coming in as is, with the tags, that doesn't break the script?

推荐答案

Javascript不支持您尝试使用的多行字符串.您必须使用+运算符或将所有文本行放入一个数组中,如下所示:

Javascript doesn't support multiline strings the way you are trying. You have to use the + opperator or put all the lines of text into an array, like so:

$('#somediv').append(
  [
    "<p>Here's my first paragraph blah blah blah</p>",
    "<p>Here's my second paragraph blah blah blah blah</p>",
    "<p>Oh here's a third paragraph blah blah</p>"
  ].join('')
);

或使用+运算符:

$('#somediv').append(
    "<p>Here's my first paragraph blah blah blah</p>" +
    "<p>Here's my second paragraph blah blah blah blah</p>" +
    "<p>Oh here's a third paragraph blah blah</p>"
);

这篇关于试图添加/添加多个段落?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆