I am getting some XML back from an AJAX call (no surprise) and I want to do something but only on certain nodes and something else on the rest. For example
我正在从一个AJAX调用中获得一些XML(这一点也不奇怪),我想做一些事情,但只在某些节点和其他节点上做一些事情。例如
<xml>
<node name="x">
</node>
<node name="x">
</node>
<node name="y">
</node>
<node name="z">
</node>
</xml>
I want all the nodes with name x to go to one table and I want all the others to go to another table.
我希望所有名称为x的节点都到一个表中,其他节点都到另一个表中。
4 个解决方案
#1
19
Use an attribute filter, in particular the attributeEquals filter:
使用属性过滤器,特别是attributeEquals过滤器:
$("node[name='x']");
To select all the other nodes, use the attributeNotEquals filter:
要选择所有其他节点,请使用attributeNotEquals过滤器:
$("node[name!='x']");
You can then apply jQuery manipulations to move these nodes elsewhere.
然后,您可以应用jQuery操作将这些节点移到别处。
Note that XPath-style selectors where deprecated in version 1.2, and have been removed altogether in jQuery 1.3.
注意,xpathstyle选择器在版本1.2中已被弃用,并且在jQuery 1.3中已被完全删除。
If you can influence what the server sends, you may want to switch to using JSON instead, you may find it easier to parse.
如果您可以影响服务器发送的内容,那么您可能希望改用JSON,您可能会发现解析起来更容易。
#2
7
success: function(xml) {
$(xml.find('node').each(function(){
if($(this).attr('name')=='x') {
//go to one table
} else {
//go to another table
}
}
}
#3
1
You can use xpath in jQuery to select the nodes:
可以使用jQuery中的xpath选择节点:
$("//node[@name='x']")
$(" / /节点[@ name = ' x ']”)
http://docs.jquery.com/DOM/Traversing/Selectors
http://docs.jquery.com/DOM/Traversing/Selectors
#4
1
jQuery accepts xpath expressions as well.
jQuery也接受xpath表达式。
$('node[name="x"]')
will select all the nodes named "node" with an attribute of "name" that has the value "x"
将选择所有名为“node”的节点,其属性为“name”,其值为“x”
#1
19
Use an attribute filter, in particular the attributeEquals filter:
使用属性过滤器,特别是attributeEquals过滤器:
$("node[name='x']");
To select all the other nodes, use the attributeNotEquals filter:
要选择所有其他节点,请使用attributeNotEquals过滤器:
$("node[name!='x']");
You can then apply jQuery manipulations to move these nodes elsewhere.
然后,您可以应用jQuery操作将这些节点移到别处。
Note that XPath-style selectors where deprecated in version 1.2, and have been removed altogether in jQuery 1.3.
注意,xpathstyle选择器在版本1.2中已被弃用,并且在jQuery 1.3中已被完全删除。
If you can influence what the server sends, you may want to switch to using JSON instead, you may find it easier to parse.
如果您可以影响服务器发送的内容,那么您可能希望改用JSON,您可能会发现解析起来更容易。
#2
7
success: function(xml) {
$(xml.find('node').each(function(){
if($(this).attr('name')=='x') {
//go to one table
} else {
//go to another table
}
}
}
#3
1
You can use xpath in jQuery to select the nodes:
可以使用jQuery中的xpath选择节点:
$("//node[@name='x']")
$(" / /节点[@ name = ' x ']”)
http://docs.jquery.com/DOM/Traversing/Selectors
http://docs.jquery.com/DOM/Traversing/Selectors
#4
1
jQuery accepts xpath expressions as well.
jQuery也接受xpath表达式。
$('node[name="x"]')
will select all the nodes named "node" with an attribute of "name" that has the value "x"
将选择所有名为“node”的节点,其属性为“name”,其值为“x”