简体   繁体   English

在由JavaScript创建的ul和li元素之间添加空格

[英]Add a space between ul and li element created by JavaScript

Been struggling with a problem now. 现在一直在努力解决问题。

I have a list that behaves quite weird, and couldt find the problem until i firebugd it and saw that the entire file prints out in one row. 我有一个行为异常奇怪的列表,直到我纵火调试它并看到整个文件都在一行中打印出来之后,才发现问题。

something like this. 这样的事情。

<div class="clock-content-wrapper"><ul class="clock-digit-wrapper hour"><li class="clock-digit-one"></li><li class="clock-digit-three"></li></ul><ul class="clock-digit-wrapper minute"><li class="clock-digit-three"></li><li class="clock-digit-seven"></li></ul><ul class="clock-digit-wrapper second"><li class="clock-digit-zero"></li><li class="clock-digit-zero"></li></ul></div>

There is no spaces between the elements. 元素之间没有空格。

Now i didn't know that could be a problem so i started to fix the elements in firebug like this. 现在我不知道这可能是个问题,所以我开始像这样修复萤火虫中的元素。

<div class="clock-content-wrapper">
<ul class="clock-digit-wrapper hour">
<li class="clock-digit-one"></li>
<li class="clock-digit-three"></li>
</ul>
<ul class="clock-digit-wrapper minute">
<li class="clock-digit-three"></li>
<li class="clock-digit-seven"></li>
</ul>
<ul class="clock-digit-wrapper second">
<li class="clock-digit-zero"></li>
<li class="clock-digit-zero"></li>
</ul>
</div>

And notice that if i have the </ul> element under my </li> the script actually works at it supposed to. 并请注意,如果我在</li>下有</ul>元素,则脚本实际上应该可以正常工作。

Is there a way to structure the file with javascript ? 有没有办法用javascript结构文件? I have a jsFiddle, where you can se the differens between the tree created in HTML and the one with JavaScript. 我有一个jsFiddle,您可以在其中区分以HTML创建的树和具有JavaScript的树之间的区别。 http://jsfiddle.net/Xk49c/ http://jsfiddle.net/Xk49c/

I really dont want to change anything in the css . 我真的不想更改CSS中的任何内容。 since both html and js application is using the same css . 因为html和js应用程序都使用相同的css。

Your <ul> elements are displayed as inline-block in your fiddle. 您的<ul>元素在小提琴中显示为行inline-block I suspect they are suffering from this little-known feature of inline-block . 我怀疑他们正在遭受这种inline-block鲜为人知的功能

The problem is, when you create the elements using HTML, you indent or separate the elements with one or more spaces, or even new lines. 问题是,当您使用HTML创建元素时,需要用一个或多个空格,甚至换行来缩进或分隔元素。 HTML renders these spaces as ONE single space and so you see the space between those elements. HTML将这些空间呈现为一个单一的空间,因此您可以看到这些元素之间的空间。

To add spaces between elements created by JavaScript, either you ll have to add a padding left to minute and second class, or insert a text node between each UL 要在JavaScript创建的元素之间添加空格,您将不得不在minutesecond类上添加一个填充,或者在每个UL之间插入一个文本节点。

hours = createElementWithClass('ul', 'clock-digit-wrapper hour');
clock_toolbar_wrapper_close.appendChild(hours);
clock_toolbar_wrapper_close.appendChild(document.createTextNode(' ')); // Add this


minutes = createElementWithClass('ul', 'clock-digit-wrapper minute');
clock_toolbar_wrapper_close.appendChild(minutes);
clock_toolbar_wrapper_close.appendChild(document.createTextNode(' ')); // And this

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

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