不显眼的Javascript来插入包含Rails RESTful URL的元素?

mwi*_*ams 2 javascript ruby jquery ruby-on-rails

我有一个简单的页面,其中包含一个渲染的iFrame.有一个名为"添加文件"的链接,并且不引人注意地我想将一个事件附加到"添加文件"锚点,以便在单击时,它会在现有的iFrame下面插入一个新的iFrame,并增加iFrame的ID.

iFrame的一个例子是:

<iframe name="uploadForm1" id="uploadForm1" src="<%= upload_file_url %>" height="50" width="800" frameborder="0" scrolling="no"></iframe>
Run Code Online (Sandbox Code Playgroud)

然后当点击"添加文件"按钮时,我最终得到:

<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>
Run Code Online (Sandbox Code Playgroud)

什么是最好的方法(jQuery或Prototype)来实现这一目标?

然而,这看起来非常微不足道,因为我需要渲染upload_file_url路由,我不太确定如何在不浪费大量时间的情况下接近它!

任何帮助表示赞赏!

Gre*_*reg 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);
}
Run Code Online (Sandbox Code Playgroud)