在 chrome 中获取 iframe 正文 html 内容

Getting iframe body html content in chrome

本文关键字:正文 html 内容 iframe 获取 chrome      更新时间:2023-09-26

我在获取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.html"></iframe>

这在Mozilla Firefox中效果很好,但在Google Chrome中它显示空白警报窗口。

可能是什么原因?

多谢!

onload添加到 iframe,然后调用 JS 函数。

您的JS:

function afterLoading(){
    var template_html = $("#get_template_html").contents().find('body').html();
    alert(template_html);
}

您的网页

<iframe id="get_template_html" src="templates/1.html" onload="afterLoading();"></iframe>