2013-03-12 152 views
1

我正在爲我父親的駕駛學校重寫網站,將不必要的iframe和圖像更改爲html5和css3標記,並通過javascript和jquery改進了功能。從jquery/javascript下載卸載文檔加載html頭文件

現在,我爲每個菜單項(對於搜索爬蟲)都有一個完整的頁面,而頁眉和菜單大部分保持不變。這就是爲什麼我想要使用歷史api,並改變內容和文檔標題和描述,當一個鏈接被點擊在啓用了JavaScript的設備上時,所以不必進行整頁刷新。

什麼工作: - 讓目標頁面的#內容股利和它加載到當前頁面 - 使用歷史API展現出新的URL和popstate事件返回到以前的狀態 - 修改內容從當前文檔

html的頭什麼行不通的: - 從目標文件獲得HTML頭

應該有這樣一個簡單的功能,對不對?或者一個很好的方法來做到這一點?

+1

當您使用jQuery將它解析爲html時,內容中省略了諸如'',''和''等標記。我建議改爲定位特定標籤,或者先將其解析爲字符串,以便僅獲取'' – 2013-03-12 14:53:09

+2

之間的內容代碼,代碼,爲什麼代碼? – 2013-03-12 14:53:09

+0

你需要從''哪些內容?我假設你想要點擊,所以你可以得到每個頁面的'''' document.title' – <span class="text-secondary"> <small> <span>2013-03-12 14:55:56</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> </div> <div class="answer-title"> <span class="text-logo margin-top-sm">A</span> <h2 class="title h4">回答</h2> </div> <div class="item-description text-md markdown-body margin-bottom-40 voidso"> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">1<i class="fa fa-thumbs-up"></i></span> <i class="fa fa-check fa-2x"></i> </div> <div class="post-offset"> <div class="answer fmt"> <p>既然它是在同一個原點,你<em>可以</em>做到這一點。這有點尷尬,儘管它與jQuery在幕後使用其ajax <code class="prettyprint-override">load</code>(我預計jQuery使用文檔片段而不是<code class="prettyprint-override">iframe</code>)不在一百萬英里之外。</p> <p>基本上,你可以創建一個關閉頁<code class="prettyprint-override">iframe</code>,內容頁面加載到那裏,然後提取任何你從它的需要,這樣的事情(完全未經優化的):</p> <pre><code class="prettyprint-override">// I'm assuming you have a variable containing the content URL; I'll use `page` var page = "/ocazuc/2"; // Create the iframe, put it off-page, and put it in the DOM var iframe = $('<iframe>'); iframe.css({ position: "absolute", left: -10000 }).appendTo(document.body); // Hook the load event on it iframe.load(function() { // Get the document var $doc = $(iframe[0].contentDocument.documentElement); // Steal its content $doc.find("body").contents().appendTo(document.body); // And use its title or whatever else you want from the `head` document.title = $doc.find('title').text(); // Done with it iframe.remove(); }); // Start loading it iframe[0].src = page; </code></pre> <p><a href="http://jsbin.com/esapuf/1" rel="nofollow">Live Example</a> | <a href="http://jsbin.com/esapuf/1/edit" rel="nofollow">Source</a></p> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/15365385">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2013-03-12 15:31:31</span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> <!-- comments --> <div class="comments"> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">謝謝,我要看看這是否有效! – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/2161281/">Gi11i4m</a></span> <span>2013-03-18 16:38:52</span> </small> </span> </p> </div> </div> </div> <div itemprop="comment" class="post-comment"> <div class="row"> <div class="col-lg-1"><span class="text-secondary">+0</span></div> <div class="col-lg-11"> <p class="commenttext">這可能不會比整頁刷新更高效,因爲您仍然在完成整個頁面刷新過程中涉及很多事情,例如,設置一個全新的窗口環境和JS範圍,獲取和解析JS,CSS,圖像,視頻,其他iframe等。理想情況下,您希望將內容作爲惰性,分離的DOM元素提取。 – <span class="text-secondary"> <small> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/127040/">greim</a></span> <span>2013-04-29 19:17:22</span> </small> </span> </p> </div> </div> </div> </div> </div> </article> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1038284119" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <article class="board-top-1 padding-top-10"> <div class="post-col vote-info"> <span class="count">0<i class="fa fa-thumbs-up"></i></span> </div> <div class="post-offset"> <div class="answer fmt"> <p>在接受的答案下查看我的意見。基本上,使用這種方法有一些性能問題,這使我尋找更好的方法。爲此,我調整了麻省理工學院許可和書面咖啡腳本的rails turbolinks項目中找到的方法。值得信賴的是他們解決一些瀏覽器兼容性問題。</p> <p><a href="https://github.com/rails/turbolinks" rel="nofollow">https://github.com/rails/turbolinks</a></p> <pre><code class="prettyprint-override">/* * A function that takes a string of HTML and returns a document object. */ var parseDocument = (function() { function createDocumentUsingParser(html) { return (new DOMParser()).parseFromString(html, 'text/html'); } function createDocumentUsingDOM(html) { var doc = document.implementation.createHTMLDocument(''); doc.documentElement.innerHTML = html; return doc; } function createDocumentUsingWrite(html) { var doc = document.implementation.createHTMLDocument(''); doc.open('replace'); doc.write(html); doc.close(); return doc; } /* * Use createDocumentUsingParser if DOMParser is defined and natively * supports 'text/html' parsing (Firefox 12+, IE 10) * * Use createDocumentUsingDOM if createDocumentUsingParser throws an exception * due to unsupported type 'text/html' (Firefox < 12, Opera) * * Use createDocumentUsingWrite if: * - DOMParser isn't defined * - createDocumentUsingParser returns null due to unsupported type 'text/html' (Chrome, Safari) * - createDocumentUsingDOM doesn't create a valid HTML document (safeguarding against potential edge cases) */ var parser; if (window.DOMParser) { try { var testDoc = createDocumentUsingParser('<html><body><p>test'); if (testDoc && testDoc.body && testDoc.body.childNodes.length === 1) { parser = createDocumentUsingParser; } } catch(ex) { parser = createDocumentUsingDOM; } } if (!parser) { parser = createDocumentUsingWrite; } return parser; })(); </code></pre> <p>那麼想必例如,你可以這樣做:</p> <pre><code class="prettyprint-override">var doc = parseDocument(wadOfHtml); var title = doc.title; var $content = $(doc).find('#content'); </code></pre> <p>被警告,我沒有驗證爲自己的代碼是否正確,我要脫鐵軌項目的信譽。我還沒有驗證我的咖啡改編是否沒有引入語法或其他錯誤。我從字面上甚至還沒有運行它;我只是樂觀。</p> </div> <div class="post-info"> <div class="post-meta row"> <p class="text-secondary col-lg-6"> <span class="source"> <a rel="noopener" target="_blank" href="https://stackoverflow.com/q/16289855">來源</a> </span> </p> <p class="text-secondary col-lg-6"> <span class="float-right date"> <span>2013-04-29 23:24:47</span> <a rel="noopener" target="_blank" href="https://stackoverflow.com/users/127040/">greim</a></span> </p> <p class="col-12"></p> <p class="col-12"></p></div> </div> </div> </article> </div> <div class="clearfix"> </div> <div class="relative-box"> <div class="relative">相關問題</div> <ul class="relative_list"> <li> 1. <a href="http://hk.uwenku.com/question/p-msngyyel-oo.html" target="_blank" title="PHP下載郵件頭(文件下載)"> PHP下載郵件頭(文件下載) </a> </li> <li> 2. <a href="http://hk.uwenku.com/question/p-ocowhawq-br.html" target="_blank" title="從文本中加載html文檔"> 從文本中加載html文檔 </a> </li> <li> 3. <a href="http://hk.uwenku.com/question/p-uetjppma-eg.html" target="_blank" title="從cloudkit下載html文件"> 從cloudkit下載html文件 </a> </li> <li> 4. <a href="http://hk.uwenku.com/question/p-vurtdkxa-qz.html" target="_blank" title="卸載加載Headjs的JS文件"> 卸載加載Headjs的JS文件 </a> </li> <li> 5. <a href="http://hk.uwenku.com/question/p-obljqfum-po.html" target="_blank" title="HTML下載文件"> HTML下載文件 </a> </li> <li> 6. <a href="http://hk.uwenku.com/question/p-hzmxrsjt-bda.html" target="_blank" title="下載HTML文件"> 下載HTML文件 </a> </li> <li> 7. <a href="http://hk.uwenku.com/question/p-vnshcqze-qn.html" target="_blank" title="下載HTML文件"> 下載HTML文件 </a> </li> <li> 8. <a href="http://hk.uwenku.com/question/p-deoephox-nv.html" target="_blank" title="從.res文件加載HTML"> 從.res文件加載HTML </a> </li> <li> 9. <a href="http://hk.uwenku.com/question/p-xaxmygkf-vn.html" target="_blank" title="不從html文檔加載圖像?"> 不從html文檔加載圖像? </a> </li> <li> 10. <a href="http://hk.uwenku.com/question/p-gxikacej-bkv.html" target="_blank" title="從Sugarcrm下載文檔"> 從Sugarcrm下載文檔 </a> </li> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block; text-align:center;" data-ad-layout="in-article" data-ad-format="fluid" data-ad-client="ca-pub-6208739752673518" data-ad-slot="4606349252"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <li> 11. <a href="http://hk.uwenku.com/question/p-orieymxu-uk.html" target="_blank" title="加載/卸載利用加載外部文件"> 加載/卸載利用加載外部文件 </a> </li> <li> 12. <a href="http://hk.uwenku.com/question/p-zcwlfswa-bhz.html" target="_blank" title="php pdf文件下載:無法加載PDF文檔"> php pdf文件下載:無法加載PDF文檔 </a> </li> <li> 13. <a href="http://hk.uwenku.com/question/p-fioxyrqq-bgx.html" target="_blank" title="將網頁html下載到html文檔"> 將網頁html下載到html文檔 </a> </li> <li> 14. <a href="http://hk.uwenku.com/question/p-ubzeamcm-dx.html" target="_blank" title="下拉HTML文件下載"> 下拉HTML文件下載 </a> </li> <li> 15. <a href="http://hk.uwenku.com/question/p-tpckgppb-bo.html" target="_blank" title="下載OpenCL頭文件"> 下載OpenCL頭文件 </a> </li> <li> 16. <a href="http://hk.uwenku.com/question/p-akmptnth-dv.html" target="_blank" title="php force下載頭文件"> php force下載頭文件 </a> </li> <li> 17. <a href="http://hk.uwenku.com/question/p-tutitybz-sv.html" target="_blank" title="PHP文件下載與頭"> PHP文件下載與頭 </a> </li> <li> 18. <a href="http://hk.uwenku.com/question/p-tysnocpp-vo.html" target="_blank" title="從另一個文件加載HTML下載菜單"> 從另一個文件加載HTML下載菜單 </a> </li> <li> 19. <a href="http://hk.uwenku.com/question/p-rxsejghw-vq.html" target="_blank" title="如何從Firebase下載HTML文件並加載到webview?"> 如何從Firebase下載HTML文件並加載到webview? </a> </li> <li> 20. <a href="http://hk.uwenku.com/question/p-fntbtjhe-bkg.html" target="_blank" title="xpages從文檔加載json"> xpages從文檔加載json </a> </li> <li> 21. <a href="http://hk.uwenku.com/question/p-zxysaocd-bhq.html" target="_blank" title="卸載工頭"> 卸載工頭 </a> </li> <li> 22. <a href="http://hk.uwenku.com/question/p-wccwtfxe-bcz.html" target="_blank" title="下載XML文檔"> 下載XML文檔 </a> </li> <li> 23. <a href="http://hk.uwenku.com/question/p-cmgpnkof-boc.html" target="_blank" title="DocuSign - 文檔下載"> DocuSign - 文檔下載 </a> </li> <li> 24. <a href="http://hk.uwenku.com/question/p-offivhqt-nu.html" target="_blank" title="下載文檔7.2"> 下載文檔7.2 </a> </li> <li> 25. <a href="http://hk.uwenku.com/question/p-dngxcphh-kb.html" target="_blank" title="下載Drupal文檔?"> 下載Drupal文檔? </a> </li> <li> 26. <a href="http://hk.uwenku.com/question/p-bblvpzvm-bbx.html" target="_blank" title="C#加載/卸載插件"> C#加載/卸載插件 </a> </li> <li> 27. <a href="http://hk.uwenku.com/question/p-hfjvcxnk-cd.html" target="_blank" title="Javascript文檔加載事件"> Javascript文檔加載事件 </a> </li> <li> 28. <a href="http://hk.uwenku.com/question/p-flfayaiv-wo.html" target="_blank" title="php文件下載頭文件"> php文件下載頭文件 </a> </li> <li> 29. <a href="http://hk.uwenku.com/question/p-scccfhbu-bdo.html" target="_blank" title="apk文件強制下載頭文件"> apk文件強制下載頭文件 </a> </li> <li> 30. <a href="http://hk.uwenku.com/question/p-cpxkemcj-nd.html" target="_blank" title="Swift3:如何從文檔目錄加載html文件到WKWebview"> Swift3:如何從文檔目錄加載html文件到WKWebview </a> </li> </ul> </div> <div> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-format="autorelaxed" data-ad-client="ca-pub-6208739752673518" data-ad-slot="1575177025"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="padding-top-10"></div> </div> </div> <script type="text/javascript" src="http://img.uwenku.com/uwenku/script/side.js?t=1644592048261"></script> <script type="text/javascript" src="http://img.uwenku.com/uwenku/plugin/highlight/highlight.pack.js"></script> <link href="http://img.uwenku.com/uwenku/plugin/highlight/styles/docco.css" media="screen" rel="stylesheet" type="text/css" /> <script type="text/javascript"> $('pre').each(function(i, e) { hljs.highlightBlock(e, "<span class='indent'> </span>", false) }); </script> <div class="col-lg-3 col-md-4 col-sm-5"> <div id="rightTop"> <div class="row"> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <ins class="adsbygoogle" style="display:block" data-ad-client="ca-pub-6208739752673518" data-ad-slot="5415218910" data-ad-format="auto" data-full-width-responsive="true"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> <div class="row sidebar panel panel-default"> <div class="panel-heading font-bold"> 最新問題 </div> <div class="m-b-sm m-t-sm clearfix"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-vyjyhjfq-bhe.html" target="_blank" title="Select2 templateResult數據沒有可供引用的元素"> Select2 templateResult數據沒有可供引用的元素 </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-wdyiixcv-bhu.html" target="_blank" title="建模Azure Cosmos DB"> 建模Azure Cosmos DB </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-gmfjrbst-bkk.html" target="_blank" title="從一個數據框中創建一個以一列值爲參考的數組"> 從一個數據框中創建一個以一列值爲參考的數組 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-xfvbmagw-bkb.html" target="_blank" title="CRM 2016升級服務 - 案例 - 環境"> CRM 2016升級服務 - 案例 - 環境 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-guqohxxm-bkx.html" target="_blank" title="圖書館是建設應用程序必不可少的?"> 圖書館是建設應用程序必不可少的? </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-cjrbhwad-zv.html" target="_blank" title="角5類型錯誤:this.tipp.isPersistent不是一個函數"> 角5類型錯誤:this.tipp.isPersistent不是一個函數 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-ywlbpvus-bak.html" target="_blank" title="Unique_ptr C++用法"> Unique_ptr C++用法 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-gjurezuu-bcq.html" target="_blank" title="如何使用Java 8和StringJoiner連接來自列表的兩個字段?"> 如何使用Java 8和StringJoiner連接來自列表的兩個字段? </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-htscpszy-bca.html" target="_blank" title="如何刷新改進2/rxJava中的ACCESS-TOKEN"> 如何刷新改進2/rxJava中的ACCESS-TOKEN </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-etgmkhto-bbu.html" target="_blank" title="VBA搜索表格排除空白單元格"> VBA搜索表格排除空白單元格 </a> </li> </ul> </div> </div> </div> <p class="article-nav-bar"></p> <div class="row sidebar article-nav"> <div class="row box_white visible-sm visible-md visible-lg margin-zero"> <div class="top"> <h3 class="title"><i class="glyphicon glyphicon-th-list"></i> 相關問題</h3> </div> <div class="article-relative-content"> <ul class="side_article_list"> <li class="side_article_list_item"> 1. <a href="http://hk.uwenku.com/question/p-msngyyel-oo.html" target="_blank" title="PHP下載郵件頭(文件下載)"> PHP下載郵件頭(文件下載) </a> </li> <li class="side_article_list_item"> 2. <a href="http://hk.uwenku.com/question/p-ocowhawq-br.html" target="_blank" title="從文本中加載html文檔"> 從文本中加載html文檔 </a> </li> <li class="side_article_list_item"> 3. <a href="http://hk.uwenku.com/question/p-uetjppma-eg.html" target="_blank" title="從cloudkit下載html文件"> 從cloudkit下載html文件 </a> </li> <li class="side_article_list_item"> 4. <a href="http://hk.uwenku.com/question/p-vurtdkxa-qz.html" target="_blank" title="卸載加載Headjs的JS文件"> 卸載加載Headjs的JS文件 </a> </li> <li class="side_article_list_item"> 5. <a href="http://hk.uwenku.com/question/p-obljqfum-po.html" target="_blank" title="HTML下載文件"> HTML下載文件 </a> </li> <li class="side_article_list_item"> 6. <a href="http://hk.uwenku.com/question/p-hzmxrsjt-bda.html" target="_blank" title="下載HTML文件"> 下載HTML文件 </a> </li> <li class="side_article_list_item"> 7. <a href="http://hk.uwenku.com/question/p-vnshcqze-qn.html" target="_blank" title="下載HTML文件"> 下載HTML文件 </a> </li> <li class="side_article_list_item"> 8. <a href="http://hk.uwenku.com/question/p-deoephox-nv.html" target="_blank" title="從.res文件加載HTML"> 從.res文件加載HTML </a> </li> <li class="side_article_list_item"> 9. <a href="http://hk.uwenku.com/question/p-xaxmygkf-vn.html" target="_blank" title="不從html文檔加載圖像?"> 不從html文檔加載圖像? </a> </li> <li class="side_article_list_item"> 10. <a href="http://hk.uwenku.com/question/p-gxikacej-bkv.html" target="_blank" title="從Sugarcrm下載文檔"> 從Sugarcrm下載文檔 </a> </li> </ul> </div> </div> </div> </div> </div> </div> </div><!-- wrap end--> <!-- footer --> <footer id="footer"> <div class="bg-simple lt"> <div class="container"> <div class="row padder-v m-t"> <div class="col-xs-8"> <ul class="list-inline"> <li><a href="http://hk.uwenku.com/contact">聯系我們</a></li> <li>© 2020 HK.UWENKU.COM</li> <li><a target="_blank" href="https://beian.miit.gov.cn/">沪ICP备13005482号-4</a></li> <li><script type="text/javascript" src="https://v1.cnzz.com/z_stat.php?id=1280101193&web_id=1280101193"></script></li> <li><a href="http://www.uwenku.com/" target="_blank" title="优文库">简体中文</a></li> <li><a href="http://hk.uwenku.com/" target="_blank" title="優文庫">繁體中文</a></li> <li><a href="http://ru.uwenku.com/" target="_blank" title="поле вопросов и ответов">Русский</a></li> <li><a href="http://de.uwenku.com/" target="_blank" title="Frage - und - antwort - Park">Deutsch</a></li> <li><a href="http://es.uwenku.com/" target="_blank" title="Preguntas y respuestas">Español</a></li> <li><a href="http://hi.uwenku.com/" target="_blank" title="कार्यक्रम प्रश्न और उत्तर पार्क">हिन्दी</a></li> <li><a href="http://it.uwenku.com/" target="_blank" title="IL Programma di chiedere Park">Italiano</a></li> <li><a href="http://ja.uwenku.com/" target="_blank" title="プログラム問答園区">日本語</a></li> <li><a href="http://ko.uwenku.com/" target="_blank" title="프로그램 문답 단지">한국어</a></li> <li><a href="http://pl.uwenku.com/" target="_blank" title="program o park">Polski</a></li> <li><a href="http://tr.uwenku.com/" target="_blank" title="Program soru ve cevap parkı">Türkçe</a></li> <li><a href="http://vi.uwenku.com/" target="_blank" title="Đáp ứng viên">Tiếng Việt</a></li> <li><a href="http://fr.uwenku.com/" target="_blank" title="Programme interrogation Park">Française</a></li> </ul> </div> </div> </div> </div> </div> </footer> <!-- / footer --> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f78a970f17b19a79fc477a3378096f29"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>