我有从父级追加到我的 iframe 的逻辑
这个有效:
$('#iframe').load(function() {
$(this).contents().find('#target').append('this text has been inserted into the iframe by jquery');
});
这不是
$('#iframe').load(function() {
$(this).contents().find('body').append('<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>');
});
.lf
问题与插入的脚本标签没有正确转义有关。 一半的 javascript 在 html 中变得可见,就像第一个脚本标记突然结束一样。
最佳答案
也许错误出在你的字符串上,永远不要在 javascript 中创建一个带有文字 </script> 的字符串。
$('#iframe').load(function() {
$(this).contents().find('body').append('<scr' + 'ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></scr' + 'ipt>');
});
关于javascript - 如何将 <script> 加载到 iframe 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3935398/