2009-08-08 8 views
1

IDを持つ4つの静的divを含むページがあります。私は、動的に生成された多くのdivを1つのdivから別のdivに移動させることによって、ドラッグ可能でドラッグ可能なスクリプトを追加して相互作用を可能にします。javascriptでどのdivがdivにあるのかを知るにはどうすればいいですか?

ダイナミックdivがどのスタティックdivに含まれているかを確認する必要があります。この情報はどのように取得できますか?

答えて

3

上記のコメント欄これを行うための大胆な方法ですが、松葉杖なしでこの方法の基本的なDOM操作を実行する方法を常に知ることは価値があります。このコードは、テストされていないが、あなたが使用したいと思います何に確かに非常に近いです:

/** 
* Finds the ancestor div element of descendantDiv which has one of 
* the given array of ids. If multiple div elements have with the 
* given ids are ancestors of descendantDiv, the most closely related 
* one (i.e. parent preferred to grandparent) is returned. If no 
* ancestor of descendantDiv is a div element with one of the ids 
* given by ids, returns null. 
* 
* @param descendantDiv : HTMLDivElement 
* the div element whose containing div with one of the provided 
* ids is to be determined 
* @param ids : [string] 
* array of ids which the desired ancestor div might have 
* @returns HTMLDivElement 
* the first parent, grandparent, etc. div element of descendantDiv 
* which has an id contained in ids 
*/ 
function findAncestorDivWithId(descendantDiv, ids) 
{ 
    for (var parent = descendantDiv.parentNode; 
     parent; 
     parent = parent.parentNode) 
    { 
    if (parent.nodeName.toLowerCase() !== "div") 
     continue; 
    for (var i = 0, sz = ids.length; i < sz; i++) 
    { 
     if (parent.id === ids[i]) 
     return parent; 
    } 
    } 
    return null; 
} 
+0

うわー!ありがとう、私はすべての組み込みのDOMの能力の大きな脂肪のチートシートのw3schoolsをチェックする必要があります。私はちょうど私の足をJavascriptで濡らし始めている。 –

1

あなたはScriptaculousのを使用することを計画しているので、あなたはプロトタイプで簡単にこれを行うことができます。

if ($("dynamicElement").descendantOf("staticElement")) 
{ 

} 
0

限り私はあなたがすべてのチャイルズ(またはすべての子divを)見つけたいあなたの質問を理解して、既知の要素ですか?

scriptaculousを使用すると、prototypeからchildElementsメソッドを使用することがあります。 詳細はhttp://prototypejs.org/api/element/childElementsを参照してください。

ようこそ。

+0

私は、複数のユーザーがドラッグと同じデータベースから削除する必要がありますので、まあ、私はdiv要素のすべての要素を検索したくありませんしたがって、データベースへの書き込みに続くすべての要素の読み取りは、誰かの変更を元に戻すことができます。これは、(今までに与えられたオプションの)最良の賭けのように、最初の答えのものと似た4つのif文です。 ありがとうございました。 (また、<3このサイト!) –

関連する問題