>>> d = PyQuery( ... ' This is a ' ... ' test ') >>> d('strong').closest('div') [ ] >>> d('strong').closest('.hello') [ ] >>> d('strong').closest('for
(self, selector=None)
| 555 | return self._filter_only(selector, elements) |
| 556 | |
| 557 | def closest(self, selector=None): |
| 558 | """ |
| 559 | >>> d = PyQuery( |
| 560 | ... '<div class="hello"><p>This is a ' |
| 561 | ... '<strong class="hello">test</strong></p></div>') |
| 562 | >>> d('strong').closest('div') |
| 563 | [<div.hello>] |
| 564 | >>> d('strong').closest('.hello') |
| 565 | [<strong.hello>] |
| 566 | >>> d('strong').closest('form') |
| 567 | [] |
| 568 | """ |
| 569 | result = [] |
| 570 | for current in self: |
| 571 | while (current is not None and |
| 572 | not self._copy(current).is_(selector)): |
| 573 | current = current.getparent() |
| 574 | if current is not None: |
| 575 | result.append(current) |
| 576 | return self._copy(result, parent=self) |
| 577 | |
| 578 | def contents(self): |
| 579 | """ |