【问题标题】:How to append current page contents to another page?如何将当前页面内容附加到另一个页面?
【发布时间】:2015-08-27 06:59:12
【问题描述】:

我有两个 HTML 页面,一个是源页面,另一个是副本,我需要将一些 div 元素传递到其他(副本)页面正文。我做了一半,这里我使用了setTimeOut函数来实现它 没有超时功能怎么办? 并且浏览器应该显示 url。

源页面:

<html>
    <head>        
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.11.3.min.js"></script>
    </head>
    <body>
        
        <script>
            function copyCon(){
                
            var p = document.getElementById("1");
            var p_prime = p.cloneNode(true);
            var w    = window.open('printcopy.html');
            
            setTimeout(function(){ 
                w.document.body.appendChild(p_prime);
            }, 1000);
            }
            
        </script>
        
        <button onclick="copyCon()">copy </button>
        <div id = "1" style="width: 500px; height: 500px; border: 1px solid black; position: relative">
            
            <h3> this is a test heading </h3>
            <p style="top: 60px; left: 20px; position: absolute;"> test Paragraph</p>
            
            <input type="text" style="position: absolute; top: 40px; left: 20px"/>
            
        </div>
        
    </body>
</html>

复制:

<html>
    <head>
    </head>
    <body>        
    </body> </html>

有没有办法访问复制页面的元素和执行功能?

【问题讨论】:

标签: javascript jquery html


【解决方案1】:

我自己找到了解决办法

function copyCon(){


   var w    = window.open('printcopy.html');
   w.onload = function(){
     var p = document.getElementById("1");
     var p_prime = p.cloneNode(true);
     w.document.body.appendChild(p_prime);
   };
 }

感谢支持

【讨论】:

    【解决方案2】:

    您可以使用 $.get 函数 (documentation) 来实现。

    在你想从复制中追加元素的文件中(假设你有一个名为div_you_want_to_be_append的div):

    $.get('source.php', function (data) {
        $('#div_you_want_to_be_append').append(data);
    });
    

    避免使用新的 HTML 标记的最佳方法是在 PHP 中包含要复制到新文件的内容,然后在源代码中调用 include 它并调用 $.get 函数(在我的示例中,文件:source.php )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-28
      • 2020-10-19
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      相关资源
      最近更新 更多