解决 jQuery 和无序列表加载问题
我正在 XHTML 中构建一个非常简单的多层深度无序列表树。它的工作方式是,您单击父节点,它使用 jQuery .load() API 向服务器发出 AJAX 回调,以查看该节点是否有子节点。如果是,则会将这些节点插入其中。当您再次单击父链接时,它会执行 .remove() 来删除子链接。
在 Safari、Chrome 和 FireFox 中一切正常。但在 IE6、IE7、IE8 和 Opera 中,它被打破了。
在 IE 中,代码在展开父母以显示孩子时起作用。但是,当我单击父级以使用 .remove() 再次隐藏子级时,它会进入子级并删除其子级,而不是本身。
在 Opera 中,代码会展开,但随后会随着展开而移动边距。然后,在删除时,它会出现与 IE 相同的问题。
是什么导致了这种奇怪的现象?
I'm building a very simplistic unordered list tree in XHTML with multiple levels deep. The way it works is that you click a parent node, and it uses the jQuery .load() API to make an AJAX call back to the server to see if this node has children. If it does, it inserts those nodes inside. When you click the parent link again, it does a .remove() to remove the children.
Everything works fine in Safari, Chrome, and FireFox. But in IE6, IE7, IE8, and Opera, it's breaking.
In the IE's, the code works when expanding parents to show children. But when I click the parents to hide the children again with the .remove(), it's going into the children and doing a remove of their children, rather than itself.
In Opera, the code expands but then moves the margins over as it is expanded. Then, on remove, it exhibits the same problem as the IEs.
What could be causing this strangeness?
Sample posted here: http://volomike.com/downloads/sample1.tar.gz
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,沃洛迈克!我查看了您的代码,发现存在一些问题:
首先,当您使用
load
时,它不会替换所选节点,而是替换其内容。因此,您在
li
上调用 load,但也在 AJAX 结果中返回相同的li
。随后,您会得到以下结果:此外,您在
ajax.php
行38
中使用两个标记来关闭它一个
ul
和一个li
。所以,如果你解决了这些问题,它就应该开始工作。也就是说,我会以完全不同的方式对待你正在做的事情。我希望这对您有所帮助:
HTML
PHP
JS/jQuery
OK, Volomike! I looked at your code an there were a few problems:
First, when you use
load
, it doesn't replace the selected node, it replaces its contents.So, you are calling load on a
li
but also returning the sameli
in your AJAX result. Subsequently, you are getting this:Also, you were closing it with two
</ul>
tags in yourajax.php
line38
instead of oneul
and oneli
.So, if you fix those things it should start working. That said, I would approach what you are doing totally differently. I hope this helps you out:
HTML
PHP
JS/jQuery