代码之家  ›  专栏  ›  技术社区  ›  mwilliams

插入包含rails restful url的元素的不引人注目的javascript?

  •  2
  • mwilliams  · 技术社区  · 16 年前

    我有一个简单的页面,只有一个呈现的iframe。有一个名为“添加文件”的链接,我不太显眼地想将一个事件附加到“添加文件”锚上,这样当单击时,它会在现有的iframe的下面插入一个新的iframe,iframe的ID会增加。

    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或原型)是什么?

    但是,这看起来很简单,因为我需要呈现上传文件的URL路径,我不太确定如何在不浪费大量时间的情况下处理它!

    感谢您的帮助!

    1 回复  |  直到 16 年前
        1
  •  5
  •   Greg    16 年前

    这应该为您做到:

    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);
    }