我正在使用ajax将内容从view_login.php加载到index.php´s div id="display"
中
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();
而不是echo $output
,我只是针对div本身。
PHP/HTML
$output = '
<div id="visible">
<form>
<input type="text" name="name"/>
<input type="submit" />
</form>
</div>';
echo $output;
是否可以只针对div visible
和它的内容而不是页面上的每一个输出?不需要使用jquery和普通/原始的javascript。
发布于 2014-06-26 12:17:49
在普通的JavaScript中有几种方法可以做到这一点。一种方法是将文档片段附加到一个单独的文档中,然后在它上使用querySelector
来提取您想要的div。
演示
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);
其他选择包括:
DOMParser
(由IE9+支持)。https://stackoverflow.com/questions/24429605
复制相似问题