gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 13:05:34 27 4
gpt4 key购买 nike

我正在获取包含 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("html") 加载 iframe,但得到的结果相同
  • 如果重要的是主页(包含我要加载的 iframe 的页面)在他的头脑中包含 JQuery 脚本。

如何将我在“数据”变量(其中包含 JQuery 脚本)中获取的 html 页面加载到 iframe 中并加载脚本?

最佳答案

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

注意:当您使用 JQuery 将字符串附加到 iframe 时,它​​会首先将字符串转换为 JQuery 对象,这将强制 JQuery 评估所有 JavaScript 标签,女巫将在其附加到之前在主页中对其进行评估内嵌框架

关于javascript - 如何在 iframe 中添加包含 jquery 脚本的 html 页面的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311603/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com