JavaScript 空格问题
在我的容器 news-variety
中,我有 4 列,我只想将所有列移至左侧并为每列设置动画以进行写入。
为此,我使用 CSS left
属性。
我编写了函数来获取编号。列,但它添加了空格并显示错误,在我的情况下,我需要 i
值 0,1,2,3,4
但此函数显示 0、2、4、6、8。
请帮我删除空格并将 i
值设置为 0,1,2,3,4
。
这是我的代码:
var newsVariety = document.getElementById('news-variety');
if(newsVariety){
var newsColums = newsVariety.childNodes;
for(i=0;i<newsColums.length;i++){
if(newsColums[i].className == "column") continue;
alert(i);
}
}
In my container news-variety
I got 4 columns, I just want to take all column in to left and animate each column to write.
For that I use the CSS left
property.
I wrote the function to get the no. of column, but it adds whites space and display wrongly, in my case I need the i
value 0,1,2,3,4
but this function shows 0, 2, 4, 6, 8
.
Please help me to remove the whites space and get the i
value to 0,1,2,3,4
.
This is my code :
var newsVariety = document.getElementById('news-variety');
if(newsVariety){
var newsColums = newsVariety.childNodes;
for(i=0;i<newsColums.length;i++){
if(newsColums[i].className == "column") continue;
alert(i);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了元素之外,您还将获得空白(文本)节点。您可以像这样排除文本节点:
但更好的解决方案是使用一个函数 (getElementsByTagName),该函数首先只为您提供元素,如下所示:
You're getting whitespace (text) nodes in addition to elements. You could exclude the text nodes like this:
but a better solution would be to use a function (getElementsByTagName) that only gives you elements in the first place, like this:
您忘记了空白在 DOM 中表示为文本元素。
You are forgetting that whitespace is represented in the DOM as text elements.