js擷取html檔案的思路及樣本

來源:互聯網
上載者:User

複製代碼 代碼如下:
function readHTML(){
$.ajax({
async:false,
url : "aa.html“,
success : function(result){
alert(result);
}
});
}

async:false,這個是做與其他的js同步的,若為true或不填將會先運行同層級的其它代碼,也就是說這裡的result為空白,只有其它代碼執行完畢後才會運行裡面的東西,result才會有值,這通常不是所需要的

表達的可能不是很清楚,自己做個測試就明了了,這是介紹此屬性的一個詳細的例子:http://www.cnblogs.com/wlmemail/archive/2010/06/22/1762765.html

註:這是使用的jQuery

使用js的話是下面的代碼
複製代碼 代碼如下:
var xmlhttp;
if (window.XMLHttpRequest) { // 相容 IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // 相容IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "aa.html", true);
xmlhttp.send();

myDiv是你輸出的位置,這個是定義在了頁面上的一個div

這是我解決問題的來源:http://stackoverflow.com/questions/8197709/javascript-read-html-from-url-into-string
參考:http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_first

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.