【问题标题】:How would I use jQuery to grab the contents of a page and render it within a div?我将如何使用 jQuery 来获取页面的内容并将其呈现在 div 中?
【发布时间】:2009-04-17 23:23:41
【问题描述】:

这是一个回复thise的问题: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);
}

有人可以快速为我编写函数吗?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    您可以为此使用方便的load() 函数:

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

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-11-01
          • 2021-12-05
          • 2012-02-26
          相关资源
          最近更新 更多