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

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

问题描述

我有一组 jQuery 元素,可以通过调用从 DOM 中获取:

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天全站免登陆