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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?