を。しかし、コード化するのはかなり簡単です。以下に定義
findClosestAncestor()
関数が先祖タグを指定された要素のクローゼットの祖先を見つける:
<!DOCTYPE html>
<html>
<head>
<title>ancestor</title>
</head>
<body>
<div id='outer'>
<div id='inner'>
<p></p>
</div>
</div>
<script type="application/dart">
import 'dart:html';
Element findClosestAncestor(element, ancestorTagName) {
Element parent = element.parent;
while (parent.tagName.toLowerCase() != ancestorTagName.toLowerCase()) {
parent = parent.parent;
if (parent == null) {
// Throw, or find some other way to handle the tagName not being found.
throw '$ancestorTagName not found';
}
}
return parent;
}
void main() {
ParagraphElement p = query('p');
Element parent = findClosestAncestor(p, 'div');
print(parent.id); // 'inner'
}
</script>
<script src="packages/browser/dart.js"></script>
</body>
</html>
そこで、基本的には、再帰的に、いくつかの条件に一致する親要素を検索したいですか?私はそれを正しく理解しましたか? – MarioP
まあ、私は実装を指示したくないですが、はい。 –
ここで私はこの機能をdart:htmlに追加するために問題にスターを付けることができますか? –