![](/img/trans.png)
[英]Insert a paragraph from an external HTML page to another HTML page with Javascript
[英]Insert external page html into a page html
我想在我的網頁中加載/插入外部html頁面。 示例:
<b>Hello this is my webpage</b>
You can see here an interresting information :
XXXX
Hope you enjoyed
XXXX應該被一個加載頁面的小腳本(盡可能小)替換,如http://www.mySite.com/myPageToInsert.html
我用jquery找到了以下代碼:
<script>$("#testLoad").load("http://www.mySite.com/myPageToInsert.html");</script>
<div id="testLoad"></div>
我不想使用外部javascript庫作為jquery ...
有兩個解決方案(至少我知道2個):
iframe - >這個不是那么推薦的
將ajax請求發送到所需的頁面。
這是一個小腳本:
<script type="text/javascript">
function createRequestObject() {
var obj;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
obj = new ActiveXObject("Microsoft.XMLHTTP");
} else {
obj = new XMLHttpRequest();
}
return obj;
}
function sendReq(req) {
var http = createRequestObject();
http.open('get', req);
http.onreadystatechange = handleResponse;
http.send(null);
}
function handleResponse() {
if (http.readyState == 4) {
var response = http.responseText;
document.getElementById('setADivWithAnIDWhereYouWantIt').innerHTML=response;
}
}
sendReq('yourpage');
//previously </script> was not visible
</script>
iframe會適合這個賬單嗎?
<b>Hello this is my webpage</b>
You can see here an interresting information :
<iframe id="extFrame" src="http://www.mySite.com/myPageToInsert.html"></iframe>
Hope you enjoyed
您可以使用普通的舊javascript設置iframe元素的src
屬性,以便將頁面切換為另一個頁面
我認為您正在尋找的是Jquery源代碼。
你可以在這里看到更多的細節$(document).ready等效沒有jQuery
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.