如何让我的 JavaScript 正确平铺此表?

发布于 2024-12-06 23:55:33 字数 1754 浏览 0 评论 0原文

var intervalID;
function autoUpdateFeed() 
{
    if( document.autoupdatefeedform.autoupdatefeed.checked == true )
    {
        intervalID = setInterval(updateFeed, 1000);
    } else {
        clearInterval(intervalID);
    }
}
function updateFeed()
{
    var oRequest;
    try {
            oRequest=new XMLHttpRequest();
        } catch (e)   {
        try {
            oRequest=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            try {
                oRequest=new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
             }
        }
        oRequest.onreadystatechange=function()
    {
        if(oRequest.readyState==4)
        {
        //Start of problem.
            var newfeedstable = document.getElementById("newfeeditems");
            var newcontent = document.createElement('tr');
            newcontent.innerHTML = oRequest.responseText;
            while (newcontent.firstChild)
        {
                newfeedstable.insertBefore(newcontent.firstChild, newfeedstable.firstChild);
            }
        //End of problem.
        }
    }
    oRequest.open("GET","back/newsfeedlatest.php",true);
    oRequest.send(null);
}

这是我和我的朋友目前正在使用的 JavaScript 代码,用于将新的状态更新添加到表中,以便在我们正在处理的社交网站上进行新的状态更新,问题是当新元素添加到表中时,它们会平铺到一边,而不是向下平铺,所以我需要知道为什么会发生这种情况以及如何解决它,有什么想法吗?欲了解更多信息,请访问该网站,您可能需要注册一个帐户,但请随意使用虚假电子邮件,直到我们再次开始使用电子邮件激活。 http://friendgrid.com/ 以下是问题的屏幕截图以供进一步参考: http://dl.dropbox.com /u/2281426/Newsfeed%20Error.bmp

var intervalID;
function autoUpdateFeed() 
{
    if( document.autoupdatefeedform.autoupdatefeed.checked == true )
    {
        intervalID = setInterval(updateFeed, 1000);
    } else {
        clearInterval(intervalID);
    }
}
function updateFeed()
{
    var oRequest;
    try {
            oRequest=new XMLHttpRequest();
        } catch (e)   {
        try {
            oRequest=new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
            try {
                oRequest=new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {
                    alert("Your browser does not support AJAX!");
                    return false;
                }
             }
        }
        oRequest.onreadystatechange=function()
    {
        if(oRequest.readyState==4)
        {
        //Start of problem.
            var newfeedstable = document.getElementById("newfeeditems");
            var newcontent = document.createElement('tr');
            newcontent.innerHTML = oRequest.responseText;
            while (newcontent.firstChild)
        {
                newfeedstable.insertBefore(newcontent.firstChild, newfeedstable.firstChild);
            }
        //End of problem.
        }
    }
    oRequest.open("GET","back/newsfeedlatest.php",true);
    oRequest.send(null);
}

Here is the JavaScript code I and my friend are currently using to add new status updates to a table for new status updates on our work-in-process social networking site, the problem is when the new elements are added to the table, they tile to the side, instead of tiling downwards, so I need to know why this is happening and how to fix it, any ideas? For more information, look at the website, you may have to make an account but feel free to use a fake email until we start using email activation again. http://friendgrid.com/
Here's a screenshot of the problem for further reference: http://dl.dropbox.com/u/2281426/Newsfeed%20Error.bmp

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

︶ ̄淡然 2024-12-13 23:55:33

问题出在 While 内的行:

newfeedstable.insertBefore(newcontent.firstChild, newfeedstable.firstChild);

第一个参数应该只有 newcontent 而没有 .firstChild,firstChild 将仅插入该元素,而不是新插入的元素创建

虽然我建议您使用 DOM 方法来修改表,但更简单的方法是:

TableElement.insertRow( 0).innerHTML = oRequest.responseText;

在您的情况下 TableElement 将是 newfeedstable,如果您以后不需要它,您可以减少为:

document.getElementById("newfeeditems").insertRow( 0).innerHTML = oRequest.responseText;

0 作为参数将使插入始终作为第一行,使用 -1 始终作为最后一行插入。

The issue would be at the line inside the While:

newfeedstable.insertBefore(newcontent.firstChild, newfeedstable.firstChild);

There the first parameter should be only newcontent without the .firstChild, the firstChild would be inserting only that element and not the newly created <TR>

Altough i suggest you to use DOM methods to modify tables, something easier like:

TableElement.insertRow( 0).innerHTML = oRequest.responseText;

TableElement in your case would be newfeedstable, which if you don't need it later you could reduce to:

document.getElementById("newfeeditems").insertRow( 0).innerHTML = oRequest.responseText;

The 0 as parameter would make the insert always as first row, use a -1 to insert always as last row.

伪心 2024-12-13 23:55:33

接下来尝试:

oRequest.onreadystatechange=function() {
    if(oRequest.readyState===4) {
      if(oRequest.status===200) { // HTTP OK
          //Start of problem.
          var newfeedstable = document.getElementById("newfeeditems");
          var newcontent = document.createElement('tr');
          newcontent.innerHTML = oRequest.responseText;
          // next lines removed: it's a problem: you download a <td> and next code insert its into table directly, without <tr>
          //while (newcontent.firstChild)
          //{
          //    newfeedstable.insertBefore(newcontent.firstChild,newfeedstable.firstChild);
          //}
          // 1) next line added, try next:
          newfeedstable.insertBefore(newcontent,newfeedstable.firstChild)
          // 2) OR IF RESPONSE CAN CONTAIN MORE THAN ONE <TD> try next (not checked code)
          while (newcontent.cells.length) {
            var td=newcontent.cells[0];
            newcontent.removeChild(td);
            var tr=document.createElement('tr');
            tr.appendChild(td);
            newfeedstable.insertBefore(tr,newfeedstable.firstChild);
          }
          //End of problem.
       }
    }
}

Try next:

oRequest.onreadystatechange=function() {
    if(oRequest.readyState===4) {
      if(oRequest.status===200) { // HTTP OK
          //Start of problem.
          var newfeedstable = document.getElementById("newfeeditems");
          var newcontent = document.createElement('tr');
          newcontent.innerHTML = oRequest.responseText;
          // next lines removed: it's a problem: you download a <td> and next code insert its into table directly, without <tr>
          //while (newcontent.firstChild)
          //{
          //    newfeedstable.insertBefore(newcontent.firstChild,newfeedstable.firstChild);
          //}
          // 1) next line added, try next:
          newfeedstable.insertBefore(newcontent,newfeedstable.firstChild)
          // 2) OR IF RESPONSE CAN CONTAIN MORE THAN ONE <TD> try next (not checked code)
          while (newcontent.cells.length) {
            var td=newcontent.cells[0];
            newcontent.removeChild(td);
            var tr=document.createElement('tr');
            tr.appendChild(td);
            newfeedstable.insertBefore(tr,newfeedstable.firstChild);
          }
          //End of problem.
       }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文