jquery(“foo bar”)vs jquery(“foo”)。find(“bar”)
我正试图遍历一个xml文档。 这不起作用(零结果):
jquery("foo bar")
这确实有效:
jquery("foo").find("bar")
知道为什么吗?
jquery("https://stackoverflow.com/questions/894418/jqueryfoo-bar-vs-jqueryfoo-findbar/foo bar")
第一个查找bar元素是https://stackoverflow.com/questions/894418/jqueryfoo-bar-vs-jqueryfoo-findbar/foo元素的后代
所以它可以在这个例子中工作
Form is surrounded by the green outline
这个使用jquery表达式进行搜索
jquery("http://sofzh.miximages.com/jquery/foo").find("bar")
从所有https://stackoverflow.com/questions/894418/jqueryfoo-bar-vs-jqueryfoo-findbar/foo元素开始,搜索后代bar元素
所以它可以在这个例子中工作
found , not here? not here found .
所以没有你的标记无法真正指明你的问题
我很好奇你是如何得到冲突的结果,所以我尝试使用标记,但是没想到重现jQuery对象本身返回的描述行为的方法。 我确实发现了一些产生这种行为的略有不同的情况,例如:
Hello World
如果你尝试这种语法(这里我拼写出“jQuery”而不是“$”)
jQuery("#span1").parents("div img").css("max-width","25%"); // no good
然后你将得不到任何结果,因为“span1”没有“div img”父级,只有“div”父级。 但这会产生一个结果:
jQuery("#span1").parents("div").find("img").css("max-width","25%"); // will work
因为它找到“span1”的父div,并从那里找到作为该父div的后代的图像。
但我的例子是一个人为的,因为在那种情况下你可能会使用
jQuery("#span1").siblings("img").css("max-width","25%"); // will also work, and is clearer
需要注意的重要一点是,您正在遍历XML。 虽然您的示例对HTML有效且相同,但我发现,至少在IE上,后代表达式不适用于XML。
事实上,如果有人能证实并解释这一点,我将不胜感激。