[英]Loading a div from a page using Ajax
Ok so I have a link set up: 好的,所以我建立了一个链接:
<a href="javascript:ajaxpage('con_ucp.php?action={LIST.action_link}', 'ey_4col3');">
it loads the page into a div id, that being: #ey_4col3
它将页面加载到div ID中,即:
#ey_4col3
The problem is it loads the entire page and its contents and all i want to load from that page is the id: #page-contents
问题是它加载整个页面及其内容,而我要从该页面加载的全部是ID:
#page-contents
Here is the ajax and js. 这是ajax和js。 I may not be using the correct method for this, in fact I'm sure I'm not.
我可能没有为此使用正确的方法,实际上我确定不是。 But if you can suggest a way to manipulate what I'm working with or even a cleaner simpler way to do this than I will be more than grateful:
但是,如果您可以提出一种方法来操纵我正在使用的东西,或者甚至可以通过一种比我更简洁的方法来做到这一点,将不胜感激:
var loadedobjects=""
var rootdomain="http://"+window.location.hostname
function ajaxpage(url, containerid){
var page_request = false
if (window.XMLHttpRequest) // if Mozilla, Safari etc
page_request = new XMLHttpRequest()
else if (window.ActiveXObject){ // if IE
try {
page_request = new ActiveXObject("Msxml2.XMLHTTP")
}
catch (e){
try{
page_request = new ActiveXObject("Microsoft.XMLHTTP")
}
catch (e){}
}
}
else
return false
page_request.onreadystatechange=function(){
loadpage(page_request, containerid)
}
page_request.open('GET', url, true)
page_request.send(null)
}
function loadpage(page_request, containerid){
if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1))
document.getElementById(containerid).innerHTML=page_request.responseText
}
function loadobjs(){
if (!document.getElementById)
return
for (i=0; i<arguments.length; i++){
var file=arguments[i]
var fileref=""
if (loadedobjects.indexOf(file)==-1){ //Check to see if this object has not already been added to page before proceeding
if (file.indexOf(".js")!=-1){ //If object is a js file
fileref=document.createElement('script')
fileref.setAttribute("type","text/javascript");
fileref.setAttribute("src", file);
}
else if (file.indexOf(".css")!=-1){ //If object is a css file
fileref=document.createElement("link")
fileref.setAttribute("rel", "stylesheet");
fileref.setAttribute("type", "text/css");
fileref.setAttribute("href", file);
}
}
if (fileref!=""){
document.getElementsByTagName("head").item(0).appendChild(fileref)
loadedobjects+=file+" " //Remember this object as being already added to page
}
}
}
Many Thanks. 非常感谢。
The jQuery .load()
function has an optional part to specify loading a page fragment. jQuery
.load()
函数具有一个可选部分,用于指定加载页面片段。 Your function would simply become: 您的功能将变成:
function ajaxpage(url, containerid) {
$('#' + containerid).load(url + ' #page-contents');
}
The $('#' + containerid)
creates a jQuery object with a reference to the DOM element that has an id
equal to the value of containerid
. $('#' + containerid)
创建一个jQuery对象,该对象引用一个id
为containerid
值的DOM元素。 Then the .load(url + ' #page-contents')
part tells jQuery to perform an AJAX call to url
, then set the HTML of the #page-contents
element in the returned HTML as the content of the previously selected element. 然后,
.load(url + ' #page-contents')
部分告诉jQuery对url
执行AJAX调用,然后将返回的HTML中#page-contents
元素的HTML设置为先前选择的元素的内容。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.