2009-03-11 1 views
2

私は単一のレンダリングされたiFrameを持つ単純なページを持っています。 「Add File」というリンクがあり、「Add File」アンカーにイベントをアタッチすることを目立たせています。クリックすると、iFrameのIDを増やして新しいiFrameを既存のものの下に挿入します。Rails RESTful URLを含む要素を挿入する邪魔にならないJavascript?

のiFrameの例は次のようになります。

<iframe name="uploadForm1" id="uploadForm1" src="<%= upload_file_url %>" height="50" width="800" frameborder="0" scrolling="no"></iframe> 

次に「ファイルを追加」ボタンをクリックしたときに、私はで終わるでしょう:最善のアプローチだろう何

<iframe name="uploadForm1" id="uploadForm1" src="<%= upload_file_url %>" height="50" width="800" frameborder="0" scrolling="no"></iframe> 
<iframe name="uploadForm2" id="uploadForm2" src="<%= upload_file_url %>" height="50" width="800" frameborder="0" scrolling="no"></iframe> 

(jQueryのかプロトタイプ)これを付ける?

しかし、私はupload_file_urlルートをレンダリングする必要があるため、非常に些細なことに思えますが、私はそれほど時間を無駄にせずにアプローチする方法があまりわかりません!

ご協力いただければ幸いです!

答えて

5

これはあなたのためにそれを行う必要があります。

var g_maxNum = 1; // The latest iframe number 

function copyIframe() 
{ 
    // Get the latest iframe 
    var iframe = document.getElementById('uploadForm' + g_maxNum); 
    g_maxNum++ 

    // Create a new iframe by cloning the existing one 
    var newIframe = iframe.cloneNode(true); 
    // Update the properties 
    newIframe.name = 'uploadForm' + g_maxNum; 
    newIframe.id = newIframe.name 

    // Insert it after the last iframe 
    iframe.parentNode.insertBefore(newIframe, iframe.nextSibling); 
} 
+0

は美しく書かれた、しかし、それがnullを返している「iframe.parentNode」に引っかかっています。今すぐトラブルシューティング。 – mwilliams

+0

私は余分な.cloneNodeをそこに持っていました(関数の最初の行) - 固定 – Greg

+0

ありがとう、ありがとう。私はYahudaのjQueryの本を持っています。本当にそれを掘り下げる必要があります。私は前にクローン機能を見たことがあるが、その考え方にはさほど近いものではないことを知っている。これは非常にエレガントなソリューションでした。ありがとう! – mwilliams

関連する問題