I'm trying to traverse an xml document. This doesn't work (zero results):
我正试图遍历一个xml文档。这不起作用(零结果):
jquery("foo bar")
this does work:
这确实有效:
jquery("foo").find("bar")
any idea why?
知道为什么吗?
3 个解决方案
#1
2
jquery("foo bar")
the first one looks for bar element to be a descendent of foo element
第一个查找bar元素是foo元素的后代
so it would work in this example
所以它可以在这个例子中工作
<foo>
<div>Form is surrounded by the green outline</div>
<label>Child:</label>
<bar name="thisone" />
<fieldset>
<label>Grandchild:</label>
<bar name="thisone2" />
</fieldset>
</foo>
This one does searching using a jquery expression
这个使用jquery表达式进行搜索
jquery("foo").find("bar")
Starts with all foo elements and searches for descendant bar elements
从所有foo元素开始,搜索后代bar元素
so it would work in this example
所以它可以在这个例子中工作
<foo><bar>found</bar>, not here?</foo>
<foo>not here <bar>found</bar>.</foo>
so without your markup can't really specify your problem
所以没有你的标记无法真正指明你的问题
#2
0
I was curious how you got conflicting results, so I experimented with markup but couldn't think of a way to reproduce the behavior you describe returned by the jQuery object itself. I did find some slightly different situations that produce such behavior, such as:
我很好奇你是如何得到冲突的结果,所以我尝试使用标记,但是没想到重现jQuery对象本身返回的描述行为的方法。我确实发现了一些产生这种行为的略有不同的情况,例如:
<div>
<span id="span1">Hello World</span>
<img src="foo" alt="bar" />
</div>
If you try this syntax (here I am spelling out "jQuery" rather than using "$")
如果你尝试这种语法(这里我拼写出“jQuery”而不是“$”)
jQuery("#span1").parents("div img").css("max-width","25%"); // no good
then you will get no result, because "span1" does not have a "div img" parent, just a "div" parent. But this will produce a result:
然后你将得不到任何结果,因为“span1”没有“div img”父级,只有“div”父级。但这会产生一个结果:
jQuery("#span1").parents("div").find("img").css("max-width","25%"); // will work
because it locates the parent div of "span1" and from there finds the image that is a descendant of that parent div.
因为它找到“span1”的父div,并从那里找到作为该父div的后代的图像。
But my example is a contrived one, because in that situation you would probably just use
但我的例子是一个人为的,因为在那种情况下你可能会使用
jQuery("#span1").siblings("img").css("max-width","25%"); // will also work, and is clearer
#3
0
The important thing to note is that you are traversing XML. While your examples are valid and equal for HTML, I've found, at least on IE, that descendant expressions don't work on XML.
需要注意的重要一点是,您正在遍历XML。虽然您的示例对HTML有效且相同,但我发现,至少在IE上,后代表达式不适用于XML。
In fact, I'd be grateful if someone could confirm and explain this.
事实上,如果有人能证实并解释这一点,我将不胜感激。
#1
2
jquery("foo bar")
the first one looks for bar element to be a descendent of foo element
第一个查找bar元素是foo元素的后代
so it would work in this example
所以它可以在这个例子中工作
<foo>
<div>Form is surrounded by the green outline</div>
<label>Child:</label>
<bar name="thisone" />
<fieldset>
<label>Grandchild:</label>
<bar name="thisone2" />
</fieldset>
</foo>
This one does searching using a jquery expression
这个使用jquery表达式进行搜索
jquery("foo").find("bar")
Starts with all foo elements and searches for descendant bar elements
从所有foo元素开始,搜索后代bar元素
so it would work in this example
所以它可以在这个例子中工作
<foo><bar>found</bar>, not here?</foo>
<foo>not here <bar>found</bar>.</foo>
so without your markup can't really specify your problem
所以没有你的标记无法真正指明你的问题
#2
0
I was curious how you got conflicting results, so I experimented with markup but couldn't think of a way to reproduce the behavior you describe returned by the jQuery object itself. I did find some slightly different situations that produce such behavior, such as:
我很好奇你是如何得到冲突的结果,所以我尝试使用标记,但是没想到重现jQuery对象本身返回的描述行为的方法。我确实发现了一些产生这种行为的略有不同的情况,例如:
<div>
<span id="span1">Hello World</span>
<img src="foo" alt="bar" />
</div>
If you try this syntax (here I am spelling out "jQuery" rather than using "$")
如果你尝试这种语法(这里我拼写出“jQuery”而不是“$”)
jQuery("#span1").parents("div img").css("max-width","25%"); // no good
then you will get no result, because "span1" does not have a "div img" parent, just a "div" parent. But this will produce a result:
然后你将得不到任何结果,因为“span1”没有“div img”父级,只有“div”父级。但这会产生一个结果:
jQuery("#span1").parents("div").find("img").css("max-width","25%"); // will work
because it locates the parent div of "span1" and from there finds the image that is a descendant of that parent div.
因为它找到“span1”的父div,并从那里找到作为该父div的后代的图像。
But my example is a contrived one, because in that situation you would probably just use
但我的例子是一个人为的,因为在那种情况下你可能会使用
jQuery("#span1").siblings("img").css("max-width","25%"); // will also work, and is clearer
#3
0
The important thing to note is that you are traversing XML. While your examples are valid and equal for HTML, I've found, at least on IE, that descendant expressions don't work on XML.
需要注意的重要一点是,您正在遍历XML。虽然您的示例对HTML有效且相同,但我发现,至少在IE上,后代表达式不适用于XML。
In fact, I'd be grateful if someone could confirm and explain this.
事实上,如果有人能证实并解释这一点,我将不胜感激。