【发布时间】:2012-04-19 07:04:13
【问题描述】:
我正在尝试看看是否有办法实现
phpQuery 上的“最接近”方法(就像它在 jQuery 上一样)。
有这种事吗?
【问题讨论】:
我正在尝试看看是否有办法实现
phpQuery 上的“最接近”方法(就像它在 jQuery 上一样)。
有这种事吗?
【问题讨论】:
来自Commonly Confused Bits Of jQuery:
最接近(选择器)
这是一个保守的秘密,但非常有用。它像 parents() 一样工作,只是它只返回一个父/祖先。以我的经验,你通常会想要检查一个元素的祖先中是否存在一个特定元素,而不是一大堆,所以我倾向于使用它而不是 parents()。
所以parents() 存在于 phpQuery 中,您可以使用源代码中的示例
提示:您可以通过使用 parents() 并将其限制为一个返回的元素来模拟最接近的()。
$($('#element1').parents('#element2').get(0)).css('background', '#f90');
【讨论】:
要真正拼出 phpquery 语法...(我花了很长时间才得到这个!)
我想从包含特定附件(媒体文件链接)的 RSS 提要中提取“项目”。
function fetch($feed ,$fname)
{
// load the file
phpQuery::newDocumentFileHTML($feed);
// Find the first enclosure that links to the file
// drill up to the parent elements to 'item' take the first (0)
// this is the 'nearest' equivalent
return pq("enclosure[url='" . $fname ."']:first")->parents('item')->xml();
}
【讨论】: