繁体   English   中英

如何从.load()的已加载元素中获取innerHTML内容

[英]How to get innerHTML content from a loaded element made by .load()

这里是

part.html:

    <div id="header">
        Hello Mars
    </div>

full.html:

    <div id="inj">
    </div>

    $('#inj').load("part.html #header"); 
    alert(document.getElementById("header").innerHTML); <-- this part is not working

如果要检查已加载的内容,则需要执行以下操作:

$('#inj').load("part.html #header",function(){
  alert(document.getElementById("header").innerHTML); 
});

检查http://api.jquery.com/load/

load命令是异步的,您必须等待内容加载后才能读取。

load可以具有在请求完成时执行的回调函数参数。

$('#inj').load('part.html', function() {
    alert(document.getElementById('inj').innerHTML);
});

或者由于您使用的是jQuery而不是getElementById

$('#inj').html()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM