文档未准备好在使用 jquery 循环的方法后插入
有没有办法使用循环在彼此后面插入单独的 DIV 元素?
现在我使用循环来做到这一点,但它不记得插入的 DIV 在使用 load 方法之前。 结果我只看到了最后一张。
该代码的目的是在您读取带有 db-id 的 cookie 后显示所有消息 首先到达页面。
摘要:
addmessage()
函数接收 xml 对象并开始循环遍历它。 每个单独的消息都会调用 newnote()
函数,该函数加载 DIV 元素并在其回调函数中执行一些操作,例如设置 db-id
。 之后它返回到addmessage
函数。
Is there a way to use a loop to insert separate DIV elements behind each other?
Right now I use a loop to do just that, but it doesn't remember the DIV inserted
before with the load method. The result is that I only see the last one.
The code is meant to show all messages, after it reads the cookie with the db-id when you
first arrive at the page.
summary:
the addmessage()
function receives the xml-object and starts looping through it. Each separate message calls the newnote()
function, which loads a DIV element and does stuff in its callback function like setting the db-id
. After that it returns to the addmessage
function.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用什么方法插入 DIV? 如果您只想将它们全部添加到您正在使用的任何容器的末尾,您可以使用
append
。 如果您需要在特定位置使用它们,我会使用before
或after
如果您需要在 itemTwo 之后插入某些内容:
如果您在循环中执行此操作并且需要要在 itemTwo 之后继续按顺序添加它们(例如),那么您可以尝试存储对最后插入的元素的引用,然后对其使用
after
函数,或者将其插入到以下元素之前项目,项目三。What method are you using to insert the DIVs? If you just want them all added to the end of whatever container you're using, you could just using
append
. If you need them in a particular location, I'd usebefore
orafter
If you needed to insert something after itemTwo:
If you're doing this in a loop and need to keep adding them sequentially after itemTwo (for example), then you could try either storing a reference to the last-inserted element, and then use the
after
function on that, or by inserting it before the following item, itemThree.假设您的问题是找到最后一个 DIV,您可以搜索 last DIV 并将其放置之后。
Assuming your problem is finding the last DIV, you can search for the last DIV and just place it after that.
我找到了解决方案,
同样在上面的帮助下,建议关闭引用
如果我尝试使用像这样的选择器计算新添加元素的数量,
结果始终为 0
但是,如果您明确地将索引号赋予 newnote() 函数,它也像预期的那样工作。 我刚刚向循环添加了一个计数器,并将其用作函数的额外参数。
选择器无法计算函数本身内新添加的元素。
至少,这是我迄今为止的理解。
I found the solution,
also with help from above, with the suggestion off having a reference
If I try to count the number off newly added elements with the selector like this
the result is always 0
BUT, if you explicitly give the indexnumber to the newnote() function, it works like it is supposed too. I just added a counter to the loop and used that, as an extra argument for the function
The selector cannot count newly added elements within the function itself.
At least, that is my understanding sofar.