代码之家  ›  专栏  ›  技术社区  ›  Sam Becker

如何使用jquery获取页面的内容并在一个DIV中呈现它?

  •  0
  • Sam Becker  · 技术社区  · 15 年前

    这是对此的一个问题: Javascript AJAX function not working in IE?

    我需要jquery这样做:

    function render_message(id)
    {
    var xmlHttp;
      xmlHttp=new XMLHttpRequest();  
      xmlHttp.onreadystatechange=function()
        {
        if(xmlHttp.readyState==4)
          {
            document.getElementById('message').innerHTML=xmlHttp.responseText;
            document.getElementById('message').style.display='';
            }
        }
        var url="include/javascript/message.php";
        url=url+"?q="+id;
      xmlHttp.open("GET",url,true);
      xmlHttp.send(null);
    }
    

    有人能帮我快速写这个函数吗?

    3 回复  |  直到 15 年前
        1
  •  5
  •   Paolo Bergantino    15 年前

    你可以用手边的 load() 功能:

    $('#message').load("include/javascript/message.php", {q: id}, function() {
        $(this).show();
    });
    

    回调函数假定 message DIV是隐藏的,您只希望在请求完成后显示它。

        2
  •  1
  •   Jason Cohen    15 年前

    $.ajax() 检索页面并访问内容。 Documentation here .

    然后使用例如 $("#yourElementId").html( myHtmlContent ) 替换HTML。 More doc here .

        3
  •  0
  •   vezult    15 年前

    使用jquery的$.load()函数( http://docs.jquery.com/Ajax/load ):

    $("#mydiv").load("a/url/here.html");