如何在iframe中添加包含jquery脚本的html页面的结果

时间:2017-02-18 05:52:00

标签: javascript jquery html iframe

我得到包含Jquery脚本的html页面的结果。 我在iframe中加载此html并且未加载jquery

$("#getQesAnsButton").click(function() {
    $.get("/getQesAns", function(data, status) {
        $("#Qs").contents().find("body").html(data);
        //$("#Qs").contents().find("html").html(data);
    });
});

我得到的html('数据')看起来:

<html>

<head>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#buttonSubmit").click(function() {
                console.log("Button Clicked");
            });
        })
    </script>
</head>

<body>
    <form id="formQues1" action="">
        <legend> Q# 1 </legend>
        <input type="radio" name="rad" value="1"> Q1A1
        <br>
        <input type="radio" name="rad" value="2"> Q1A2
        <br> </form>
    <form id="formQues2" action="">
        <legend> Q# 2 </legend>
        <input type="radio" name="rad" value="1"> Q2A1
        <br>
        <input type="radio" name="rad" value="2"> Q2A2
        <br> </form>
    <button id="buttonSubmit"> Submit </button>
</body>

</html>
  • 正如您所看到的,我尝试使用find加载iframe(&#34; html&#34;)但我得到的结果相同
  • 如果主要页面(包含我试图加载的iframe的页面)在他的头脑中包含JQuery脚本。

如何加载html页面我收到的数据&#39; varable(包含他脑中的JQuery脚本)到iframe中,脚本会加载吗?

1 个答案:

答案 0 :(得分:0)

棘手的部分是你应该在使用它之前将响应作为字符串替换每个脚本标记到其他地方,在这里检查我对这个问题的回答:https://stackoverflow.com/a/7002297/427622

注意:当您使用JQuery将字符串附加到iframe时,它将首先将字符串转换为JQuery对象,这将强制JQuery评估所有JavaScript标记,这些标记将在主页中进行评估,然后再将其附加到iframe