JavaScript 空格问题

发布于 2024-10-19 02:46:21 字数 582 浏览 3 评论 0原文

在我的容器 news-variety 中,我有 4 列,我只想将所有列移至左侧并为每列设置动画以进行写入。

为此,我使用 CSS left 属性。

我编写了函数来获取编号。列,但它添加了空格并显示错误,在我的情况下,我需要 i0,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有深☉意 2024-10-26 02:46:21

除了元素之外,您还将获得空白(文本)节点。您可以像这样排除文本节点:

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;
            if(newsColums[i].nodeType == 3) continue;
            alert(i);
        }
    }

但更好的解决方案是使用一个函数 (getElementsByTagName),该函数首先只为您提供元素,如下所示:

var newsVariety = document.getElementById('news-variety');
    if(newsVariety){
        var newsColums = newsVariety.getElementsByTagName('whatever-your-element-tag-is');
        for(i=0;i<newsColums.length;i++){
            if(newsColums[i].className == "column") continue;
            alert(i);
        }
    }

You're getting whitespace (text) nodes in addition to elements. You could exclude the text nodes like this:

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;
            if(newsColums[i].nodeType == 3) continue;
            alert(i);
        }
    }

but a better solution would be to use a function (getElementsByTagName) that only gives you elements in the first place, like this:

var newsVariety = document.getElementById('news-variety');
    if(newsVariety){
        var newsColums = newsVariety.getElementsByTagName('whatever-your-element-tag-is');
        for(i=0;i<newsColums.length;i++){
            if(newsColums[i].className == "column") continue;
            alert(i);
        }
    }
千柳 2024-10-26 02:46:21

您忘记了空白在 DOM 中表示为文本元素。

if (newsVariety) {
  var newsColums = newsVariety.childNodes;
    for (col = 0, i = 0; i < newsColums.length; i++) {
      if (newsColums[i].className == "column")
        continue;
        alert (col++);
     }
 }

You are forgetting that whitespace is represented in the DOM as text elements.

if (newsVariety) {
  var newsColums = newsVariety.childNodes;
    for (col = 0, i = 0; i < newsColums.length; i++) {
      if (newsColums[i].className == "column")
        continue;
        alert (col++);
     }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文