我的 XML 看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<projects>
<project
id="1"
thumb="media/images/thumb.jpg"
>
<categories>
<id>1</id>
<id>2</id>
</categories>
<director>Director name</director>
<name><![CDATA[IPhone commercial]]></name>
<url><![CDATA[http://www.iphone.com]]></url>
<description><![CDATA[Description about the project]]></description>
<thumb><![CDATA[/upload/images/thumb.jpg]]></thumb>
</project>
</projects>
但是我不知道如何根据类别 ID 过滤项目?有人知道该怎么办吗? :)
类似于:
projects.project.(categories.(id == 3))
仅返回所有项目:(
最佳答案
这是一种不使用任何自定义函数的更好方法:
projects.project.(categories.id.contains(1))
contains
采用单个值来检查 XML 或 XMLList 对象。
您可以使用额外的函数来进行处理:
// check if any of the <id> nodes matches any of the given values
function containing(nodes, values) {
for each(var id in nodes) {
if(values.indexOf(parseInt(id)) !== -1) return true;
}
return false;
}
projects.project.(containing(categories.id, [1])); // matches the first project
projects.project.(containing(categories.id, [46])); // matches nothing
关于javascript - e4x 过滤器适用于多个 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2863952/