简体   繁体   English

在页面的iframe中插入div和脚本

[英]insert a div and a script into an iframe on a page

I have a page that has an iframe on it and am using jQuery to try to add an additional script into the iframe and an empty div at the end. 我的页面上有一个iframe,并且正在使用jQuery尝试将其他脚本添加到iframe中,并在最后添加一个空div。

The Iframe code is: Iframe代码为:

<iframe class="kaltura" style="width: 608px; height: 402px;" title="EPI-522_Orav_Survival_Review_Part 1_Study Elements-H264 MP4 2Mbit 16x9 [22:17]" src="/courses/2873/external_tools/retrieve?borderless=1&amp;url=https://hsphprod.kaf.kaltura.com/browseandembed/index/media/entryid/1_rs4bvlrn/showDescription/false/showTitle/false/showTags/false/showDuration/false/showOwner/false/showUploadDate/false/playerSize/608x402/playerSkin/30101351/" width="300" height="150" allowfullscreen="allowfullscreen" webkitallowfullscreen="webkitallowfullscreen" mozallowfullscreen="mozallowfullscreen"></iframe>

So far the script i have is: 到目前为止,我拥有的脚本是:

jQuery(document).ready(function ($) {
    // for each of the kaltura iframes
    $('.kaltura').each(function (index) {
        // find the body and add these items after
        $(this).contents().find('body').append('<div id="threeplay-div"></div>');
        // create the script element and insert it into the page
        $(this).contents().find('#threeplay-div').append($("<script />", {
            type: "text/javascript",
            async: true,
            src: "//static.3playmedia.com/p/projects/11439/files/831301/plugins/10616.js"
        }));
    });
});

but it does not detect the iframes when run, nor does it append the div or the script to it. 但它在运行时不会检测iframe,也不会在其上附加div或脚本。 Anyone have an idea how to get this working? 任何人都有一个想法如何使它工作?

Your iframe content might have not loaded on document.ready. 您的iframe内容可能尚未加载到document.ready上。 You should attach the callback on iframe load. 您应该在iframe加载时附加回调。 like this $(iframe).on('load',callback); 像这样的$(iframe).on('load',callback);

If the iframe src is of different domain from yours, then you cannot access the iframe contents from your code. 如果iframe src与您的域不在同一个域中,那么您将无法从代码中访问iframe内容。 Thats the security provided by browsers to externally loaded documents. 这就是浏览器为外部加载的文档提供的安全性。

See the answer for question: 看到问题的答案:
Replacing HTML within an iframe jquery 在iframe jQuery中替换HTML

The sample provided by you doesn't append div and script within iframe , instead it appends the div in the current window only. 您提供的示例未在iframe添加divscript ,而是仅在当前窗口中添加了div The sample code (shown below) appends script tag within iframe and jquery is used to create script tag. 示例代码(如下所示)将script标记附加到iframe而jquery用于创建script标记。

jQuery(document).ready(function ($) {
        // for each of the kaltura iframes
         $('iframe.class').each(function (index) {
               // find the body and add these items after
                $(this).contents().find('body').append('<div id="inserted-div"></div>');
                // create the script element and insert it into the page
                  $(this).contents().find('#inserted-div').append($("<script />", {
                    type: "text/javascript",
                    //src : src,
                    async: true,
                    src: "//www.example.com/javascript.js"
                }));
        });
});

Try this jsfiddle example and use f12 (developer tools) to verify. 试试这个jsfiddle示例,并使用f12 (开发人员工具)进行验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM