doucaigai7176 2014-06-26 11:42
浏览 79
已采纳

使用ajax将内容加载到div中

I'm Loading in content from view_login.php into index.php´s div id="display" using ajax javascript

var hr=new XMLHttpRequest();
hr.open("GET", 'view_login.php', true);
hr.onreadystatechange = function(){
    if(hr.readyState == 4 && hr.status == 200){
        var return_data = hr.responseText;
        document.getElementById('display').innerHTML = return_data;
    }
}
hr.send();

Rather than echo $output i would just target the div itself.

PHP/HTML

$output = '
<div id="visible">
<form>
<input type="text" name="name"/>
<input type="submit" />
</form>
</div>';

echo $output;

is it possible to just target the div visible and it contents instead of every ouput on the page? without using jquery and just plain/raw javascript.

  • 写回答

1条回答 默认 最新

  • dougai3418 2014-06-26 12:17
    关注

    There are a few ways of doing this in plain JavaScript. One way would be to append the HTML to a Document Fragment, which is a separate document, then use querySelector on it to pull out just the div you want.

    Demo

    var frag = document.createDocumentFragment();
    var div = document.createElement('div'); // create a div to contain the HTML
    div.innerHTML = return_data; // insert HTML to the div
    frag.appendChild(div); // append the div to the document fragment
    
    var visibleDiv = frag.querySelector("#visible"); // find the #visible div
    
    // append the div to the main document
    document.getElementById('display').appendChild(visibleDiv);
    

    Other options are:

    • DOMParser (supported by IE9+).
    • Append it as a div to the main document, but you'll need to make sure it's hidden from view, then get the div and remove it.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Qt中实现子线程和管理线程类之间实时通信
  • ¥15 cacls 命令如何解除锁定文件夹?
  • ¥50 C++使用TWAIN协议如何实现A3幅面扫描仪扫描A4横向
  • ¥15 如何在sql server里完成筛选
  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?