2013-05-08 5 views
6

jQueryにはツリーに最も近い一致する祖先を返す "closest"があります。ダーツ相当品はありますか?私は、次が壊れにくくしたいのですが:は、ダーツのjqueryの "closest"に相当します。

e.target.parent.parent.nextElementSibling.classes.toggle('hide'); 

おそらくのようなもの:組み込み関数は、私の知る限りではありません

e.target.closest('div').nextElementSibling.classes.toggle('hide'); 
+0

そこで、基本的には、再帰的に、いくつかの条件に一致する親要素を検索したいですか?私はそれを正しく理解しましたか? – MarioP

+0

まあ、私は実装を指示したくないですが、はい。 –

+0

ここで私はこの機能をdart:htmlに追加するために問題にスターを付けることができますか? –

答えて

4

を。しかし、コード化するのはかなり簡単です。以下に定義

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> 
+1

可能な限り最小限のコードサンプルを作成するか、コードをスクリプトタグに分割することをお勧めします。 –