Javascript 在chrome中获取iframe主体html内容
我在获取iframe的HTML内容时遇到问题 JQuery:Javascript 在chrome中获取iframe主体html内容,javascript,jquery,html,Javascript,Jquery,Html,我在获取iframe的HTML内容时遇到问题 JQuery: $( document ).ready(function() { var template_html = $("#get_template_html").contents().find('body').html(); alert(template_html); }); HTML <iframe id="get_template_html" src="templates/1.htm
$( document ).ready(function() {
var template_html = $("#get_template_html").contents().find('body').html();
alert(template_html);
});
HTML
<iframe id="get_template_html" src="templates/1.html"></iframe>
这在Mozilla Firefox中运行良好,但在Google Chrome中显示空白警报窗口
原因可能是什么
非常感谢 向iframe添加
onload
,并调用JS函数
你的JS:
function afterLoading(){
var template_html = $("#get_template_html").contents().find('body').html();
alert(template_html);
}
你的HTML
<iframe id="get_template_html" src="templates/1.html" onload="afterLoading();"></iframe>
听起来好像iframe没有完全加载,请尝试使用而不是文档就绪:$(window')。on('load',function(){…})
或仅绑定iframe onload事件并设置逻辑,尝试使用
,但没有成功。延迟
在这种情况下对您没有帮助。我应该尝试检查iframe是否已完全加载吗?是的,您应该使用:$(函数(){(“#获取模板_html”)。在('load',函数(){alert($(this.contents().find('body').html());})代码>上帝保佑你:)