如何让我的 JavaScript 正确平铺此表?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在 While 内的行:
第一个参数应该只有
newcontent
而没有.firstChild
,firstChild 将仅插入该元素,而不是新插入的元素创建虽然我建议您使用 DOM 方法来修改表,但更简单的方法是:
在您的情况下 TableElement 将是 newfeedstable,如果您以后不需要它,您可以减少为:
0 作为参数将使插入始终作为第一行,使用 -1 始终作为最后一行插入。
The issue would be at the line inside the While:
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 in your case would be newfeedstable, which if you don't need it later you could reduce to:
The 0 as parameter would make the insert always as first row, use a -1 to insert always as last row.
接下来尝试:
Try next: