javascript - 在 iFrame 问题中注入(inject) HTML、CSS 和 JAVASCRIPT

标签 javascript jquery html

我正在尝试构建类似于 Tinkerbin 或 JSfiddle 的东西。 它用于个人项目。

这是我现在得到的:

http://mediodesign.ca/cms-test/

一切正常,除了 JAVASCRIPT 选项卡。我遵循了 Nettuts 上的“如何将自定义 HTML 和 CSS 注入(inject) iframe”教程(http://net.tutsplus.com/tutorials/html-css-techniques/how-to-inject-custom-html-and-css -into-an-iframe/).

这是我修改后的 javascript 代码:

(function() {
$('.grid').height( $(window).height() );    

var contents = $('iframe').contents(),
    body = contents.find('body'),
    styleTag = $('<style></style>').appendTo(contents.find('head')),
    jsTag = $('<script><\/script>').appendTo(contents.find('head'));

$('textarea').keyup(function() {
    var $this = $(this);
    if ( $this.attr('id') === 'html') 
    {
    body.html( $this.val() );
    } 
    else
    {
    styleTag.text( $this.val() ), 
    jsTag.text( $this.val() );
    }

});  })  ();

我添加了 jsTag = $('<script><\/script>').appendTo(contents.find('head'));线和 jsTag.text( $this.val() ); .

我很确定它与 jsTag = $('<script><\/script>') 有关部分。 为什么是<style></style>正在工作,但 <script></script>不是吗?

请帮我解决这个问题,过去 2 小时我一直在处理这个问题!

再次感谢

最佳答案

我不是很喜欢 jQuery,所以我用纯 Javascript 做了这个解决方案(我稍后会在 jQuery 中尝试):

var contents = $('iframe').contents(),
    body = contents.find('body'),
    styleTag = $('<style></style>').appendTo(contents.find('head')),
    jsTag = document.createElement('script');
contents.find('head')[0].appendChild(jsTag);

关于javascript - 在 iFrame 问题中注入(inject) HTML、CSS 和 JAVASCRIPT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10475775/

相关文章:

javascript - 关于固定 header 的 jQuery + CSS : How to scroll smooth,?

javascript - Angular 6 中具有相应组件的子路由

javascript - JS : insert a line break into copied text

javascript - $_POST 大字符串卡住

jquery - 各种 jQuery 模态窗口插件的优缺点?

jquery - 使用jQuery选择指定属性最小值的元素

javascript - iframe 导致 css 弹出窗口出现问题

javascript - Yii2 - 将 ID 传递给 GridView 中的另一个属性

javascript - AngularJS 无法上传文件 Express

jquery - 同位素插件角印问题