Ajax替换div内容

时间:2012-03-26 00:49:47

标签: javascript ajax html javascript-events

我对javascript和ajax有点新鲜所以请耐心等待。当我点击链接时,我正在尝试使用ajax函数替换div中的内容。我一直收到错误“ajaxpage未定义”。 ajaxpage是我在js中的功能。这是我在以下链接中找到的一些示例代码: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm非常感谢任何帮助。

链接代码:

<a href="javascript:ajaxpage('about.html', 'content');">About</a>

目:

<script type=”text/javascript” src="/js/ajax.js"></script>

要替换的Div

<div id="content">
</div>

ajax.js文件:

< script type = "text/javascript" >

/***********************************************
* Dynamic Ajax Content- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

var bustcachevar = 1
//bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadedobjects = ""
var rootdomain = "http://" + window.location.hostname
var bustcacheparameter = ""

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)
    }
    if (bustcachevar)
    //if bust caching of external page
    bustcacheparameter = (url.indexOf("?") != -1) ? "&" + new Date().getTime() : "?" + new Date().getTime()
    page_request.open('GET', url + bustcacheparameter, 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
        }
    }
}

 < /script>/

1 个答案:

答案 0 :(得分:0)

更新:

我在这里为你做了一个演示:http://jsfiddle.net/sg552sg552/wsSCF/8/,我认为它有效。

P.S。我建议你使用jQuery.ajax或jQuery.load。这是jQuery版本的演示: http://jsfiddle.net/sg552sg552/YAUeA/12/

有关更多详细信息,请参阅jQuery的官方文档:http://api.jquery.com/load/

-----------------旧答案----------------------- 你有没有在你的页面中引用“ajax.js”文件? e.g。

<html>
  <head>
    <script src="ajax.js" type="text/javascript"></script>
  </head>
  <!-- body div... -->
</html>