我需要在 iframe 中运行 JavaScript 代码。但是创建 iframe 后加载了 id 为“us”的脚本。如何在 iframe 中运行此 javascript?
<iframe id="preview">
#document
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script id="us" type="text/javascript">
$("#preview").ready(function() {
$(".test").click(function() {
alert(true);
});
});
</script>
</head>
<body>
<style></style>
<div class="test"></div>
</body>
</html>
</iframe>
提前致谢。
最佳答案
IFrame 无法访问其外部的内容。 IFrame 内的所有内容都是单独的页面,因此您可以像这样威胁它。所以你在里面准备好你的文档。
// Since this is page you wait until it's loaded
$(function() {
$(".test").click(function() {
alert(true);
});
});
关于iframe 中的 JavaScript 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23274389/