使用jQuery .append()时在元素之间添加换行符或空格 [英] Add line breaks or spaces between elements when using jQuery .append()

查看:445
本文介绍了使用jQuery .append()时在元素之间添加换行符或空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通过调用从DOM中获取的jQuery元素集:

I have a jQuery set of elements that I get from my DOM by calling:

$(".some-selector");

我所有的元素都是DIV,每个元素都在自己的行中.我的DIV设置为CSS(除其他外)

All my elements are DIVs each in its own line. My DIVs are set CSS (among other things)

display: inline-block;

防止它们呈现为块元素(每个元素都在其自己的行中).

which prevents them from rendering as block elements (each in its own line).

问题在于,渲染这些DIV时,它们之间会有空格,因为每个元素之间的文档中都有换行符.我对此感到满意.我当然可以使用 float:left 来摆脱这些空间,但这不是我想要的,因为我在容器大小调整等方面会遇到其他问题.

The problem is that when these DIV are rendered they have spaces between them because there's line break in the document between each element. I'm comfortable with that. I could of course use float:left that would get rid of these spaces but that's not what I want because I would have other problems with container sizing etc.

所以.问题是我在jQuery集中操纵了这些元素的顺序,然后重新呈现它们.我基本上要做的是:

So. The problem is that I manipulate the order of these elements in my jQuery set an then rerender them. What I essentially do is:

$(".some-selector").detach().manipulate().appendTo(".container");
// or equivalent
$(".container").append($(".some-selector").detach().manipulate());

问题在于它们重新插入了DOM,但是没有换行符或空格...

The problem is that they get re-inserted into the DOM, but without line breaks or spaces...

在将元素附加到DOM时如何重新获得这些换行符?

推荐答案

重新插入元素后,请使用 .after() :

After you re-insert your elements, append some whitespace to each of them using .after():

$(".some-selector").after(" ");

http://jsfiddle.net/z5cFw/

这篇关于使用jQuery .append()时在元素之间添加换行符或空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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