简体   繁体   English

使用内联块和文本对齐的DOM Equidistant div在通过JS插入时将不起作用

[英]DOM Equidistant divs with inline-blocks and text justify won't work when inserting through JS

I have been trying to implement Chris Coyier's inline-block + text justify solution to automatically distribute div's horizontally in a given wrapper's width. 我一直在尝试实现Chris Coyier的内联块+文本对齐解决方案,以自动在给定包装器的宽度中水平分配div。 http://css-tricks.com/equidistant-objects-with-css/ http://css-tricks.com/equidistant-objects-with-css/

This works great when the elements are imediately loaded into the DOM but for some reason fails when adding elements programattically through JS. 当元素被立即加载到DOM中时,这很有效,但由于某种原因,在通过JS以编程方式添加元素时失败。 It's as if the browser just ignores the css properties. 就像浏览器只是忽略了css属性一样。

Check out this fiddle for a very basic example: http://jsfiddle.net/xmajox/NUJnZ/ The first two rows are added on HTML load. 看看这个小提琴的一个非常基本的例子: http//jsfiddle.net/xmajox/NUJnZ/前两行是在HTML加载上添加的。 Click the button to add more in run time through JS. 单击按钮可通过JS在运行时添加更多内容。

Initially I thought it could be somehow related to the use of pseudo-element :after so I tried a different version with a DOM node instead: http://jsfiddle.net/xmajox/wnPSA/ Unfortunately it reacts exactly the same way. 最初我认为它可能与使用伪元素有某种关系:之后我尝试使用DOM节点的不同版本: http//jsfiddle.net/xmajox/wnPSA/不幸的是它的反应完全相同。

Anyone has any idea why this happens? 任何人都知道为什么会这样? or better yet, how to fix/prevent it? 或者更好的是,如何修复/预防?

There needs to be some form of whitespace between the elements for this to work, see this: http://jsfiddle.net/NUJnZ/22/ 元素之间需要有某种形式的空白才能实现,请看: http//jsfiddle.net/NUJnZ/22/

Notice in the third row that there's no space between the DIVs. 请注意,在第三行中,DIV之间没有空格。 When jQuery appends the new element there's no leading or trailing whitespace. 当jQuery追加新元素时,没有前导或尾随空格。

Simply add a trailing space with .before(' ') and the elements line up properly. 只需添加带有.before(' ')的尾随空格,并正确排列元素。 (if you use .after(' ') there's a spacing issue on the last element) (如果你使用.after(' ') ,那么最后一个元素就会出现间距问题)

Updated fiddle: http://jsfiddle.net/NUJnZ/24/ 更新小提琴: http//jsfiddle.net/NUJnZ/24/

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM