简体   繁体   English

jQuery将Div内容从当前页面加载到另一个页面

[英]jQuery Load Div Content from Current Page to Another Page

I have seen some example of loading HTML content to page from an outside of the current page like 我看过一些从当前页面外部将HTML内容加载到页面的示例,例如

$("#divData").load("article.html #target");

Now I need to do some thing completely visa versa! 现在,我需要做一些完全相反的事情! I mean I need to export a portion of my current active page to an existing .php page on the server. 我的意思是我需要将当前活动页面的一部分导出到服务器上现有的.php页面。 For example if I have an HTML like 例如,如果我有一个类似HTML

<!DOCTYPE HTML>
<html>
<body>
  <div class="well"> 
   <div class="btn-group">
    <button class="btn">Left</button>
    <button class="btn">Middle</button>
    <button class="btn">Right</button>
   </div>
   <button id="loader" class="btn btn-default">Load To PHP Page</button>
  </div>
</body>
</html> 

how can I export/Load the well content with it's entire elements to a .PHP lets say print.php page which is like this: 我怎样才能将well内容及其整个元素导出/加载到.PHP可以说print.php页面,如下所示:

<?php

    require_once("dompdf_config.inc.php");

    $codigo= '<html>
                <body>
                </body>
              </html>';

    $codigo = utf8_decode($codigo);
    $dompdf = new DOMPDF();
    $dompdf->load_html($codigo);
    ini_set("memory_limit","32M");
    $dompdf->render();
    $dompdf->stream("ejemplo.pdf");

Update: 更新:

Data returned from PHP: %PDF-1.3
1 0 obj
<< /Type /Catalog
/Outlines 2 0 R
/Pages 3 0 R >>
endobj
2 0 obj
<< /Type /Outlines /Count 0 >>
endobj
3 0 obj
<< /Type /Pages
/Kids [6 0 R
]
/Count 1
/Resources <<
/ProcSet 4 0 R
/Font << 
/F1 8 0 R
>>
>>
/MediaBox [0.000 0.000 612.000 792.000]
 >>
endobj
4 0 obj
[/PDF /Text ]
endobj
5 0 obj
<<
/Creator (DOMPDF)
/CreationDate (D:20140425170443+00'00')
/ModDate (D:20140425170443+00'00')
>>
endobj
6 0 obj
<< /Type /Page
/Parent 3 0 R
/Contents 7 0 R
>>
endobj
7 0 obj
<<
/Length 71 >>
stream

0.000 0.000 0.000 rg
BT 34.016 746.579 Td /F1 12.0 Tf  [(salam)] TJ ET
endstream
endobj
8 0 obj
<< /Type /Font
/Subtype /Type1
/Name /F1
/BaseFont /Times-Roman
/Encoding /WinAnsiEncoding
>>
endobj
xref
0 9
0000000000 65535 f 
0000000008 00000 n 
0000000073 00000 n 
0000000119 00000 n 
0000000273 00000 n 
0000000302 00000 n 
0000000416 00000 n 
0000000479 00000 n 
0000000600 00000 n 
trailer
<<
/Size 9
/Root 1 0 R
/Info 5 0 R
>>
startxref
710
%%EOF

If you want the whole state of the active page, you can just submit a form that has an input wich contains the HTML you want. 如果您想要活动页面的整个状态,则只需提交一个表单,该表单的输入将包含所需的HTML。 You can also do the same in AJAX if that's what you want. 如果您要这样做,也可以在AJAX中执行相同的操作。

Eg 例如

HTML HTML

<form action="your_page.php" method="post">
    <input type="hidden" name="htmlContent">
    <button type="submit">Submit HTML</button>
</form>

JS JS

$('form').submit(function (e) {
    $(this).children('[name=htmlContent]').val($('.well').html());
});

Two options, first to answer your question, you could send the contents of div.well with ajax to print.php : 有两个选择,首先回答您的问题,您可以将div.well的内容与ajax发送到print.php

$.ajax({
    url: 'print.php',
    type: 'POST',
    data: {
        yourData: $('div.well').html()
    },
    success: function(msg) {
        alert('Data returned from PHP: ' + msg);
        // now do something with your returned data, liked load it in a new window.
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert('AJAX request failed! ' + xhr.status + ' ' + thrownError);
    }
});

You can then echo $_POST['yourData']; 然后,您可以echo $_POST['yourData']; on your print.php page to use the div.well on that page. 在您的print.php页面上使用print.php面上的div.well

Or you could just put your .load("page.html div.well") on your print.php page if the loaded content is static (see comments below). 或者,如果加载的内容是静态的,则可以将.load("page.html div.well")放在print.php页面上(请参见下面的注释)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM