使用 AJAX 加载网页的一部分 - 无 JQuery
我知道有一种 JQuery 方法可以做到这一点,但这不是我现在需要的。
我有以下 javascript 将页面内容拉入 div,但是我不需要整个页面,只需要该页面中的 DIV 内容:
function ahah(url, target) {
document.getElementById(target).innerHTML ='<img src="ajax-loader.gif"/>';
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req != undefined) {
req.onreadystatechange = function() {ahahDone(url, target);};
req.open("GET", url, true);
req.send("");
}
}
function ahahDone(url, target) {
if (req.readyState == 4) { // only if req is "loaded"
if (req.status == 200) { // only if "OK"
document.getElementById(target).innerHTML = req.responseText;
} else {
document.getElementById(target).innerHTML=" Error:\n"+ req.status + "\n" +req.statusText;
}
}
}
function load(name, div) {
ahah(name,div);
return false;
}
然后我这样称呼它:
<a href="page.php" onclick="load('page.php','ajaxcontent');return false;" class="slide_more">LOAD</a>
应该在哪里指定我想要的选择器从 page.php 加载?
I know that there is a JQuery way of doing this but that's not what I need right now.
I have the following javascript that pulls a page content into a div, however I don't want the whole page, just the content of a DIV from that page:
function ahah(url, target) {
document.getElementById(target).innerHTML ='<img src="ajax-loader.gif"/>';
if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (req != undefined) {
req.onreadystatechange = function() {ahahDone(url, target);};
req.open("GET", url, true);
req.send("");
}
}
function ahahDone(url, target) {
if (req.readyState == 4) { // only if req is "loaded"
if (req.status == 200) { // only if "OK"
document.getElementById(target).innerHTML = req.responseText;
} else {
document.getElementById(target).innerHTML=" Error:\n"+ req.status + "\n" +req.statusText;
}
}
}
function load(name, div) {
ahah(name,div);
return false;
}
And then I call it like this:
<a href="page.php" onclick="load('page.php','ajaxcontent');return false;" class="slide_more">LOAD</a>
Where should specify the selector I want to load from page.php?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常在 AJAX 应用程序中,为了获取 HTML 片段,服务器会返回该片段,而不是在客户端选择它。例如,请参阅第 4 节“酷 AJAX 示例”中的简单示例。下面提供了该网站的代码供您参考:
请注意,PHP 页面仅返回一个仅包含
的 HTML 片段,而不是完整的 html。这是 AJAX 调用的好处之一,您不需要返回整个页面,从而节省了带宽成本,因为有效负载较小
Normally in AJAX application, in order to get the HTML Fragment the server is the one who is returning the fragment instead of having it selected on the client's side. For example, see this Simple Example on Section 4. Cool AJAX example. Code from the website is provided below for your reference:
Notice that the PHP page is just returning an HTML Fragment containing the
<span>
only instead of the full html. This is one of the benefits of AJAX call that you don't need to return the full page thereby saving the bandwidth cost since the payload is smaller