jQuery iframe 和灯箱效果

发布于 2024-12-16 13:13:52 字数 152 浏览 1 评论 0原文

我在同一个域的 iframe 中保存了许多图像,但我希望能够对图像应用单击功能,以便当单击它时,它会从 iframe 中弹出并进入父正文。

理想情况下,我不想将 jQuery 插入 iframe 主体中,只需将其存在于父主体中即可。

有人能想到我该怎么做吗?

I have a number of image held in an iframe, on the same domain, but I want to be able to apply a click function to the image so that when its clicked it pops out of the iframe and into the parent body.

Ideally I dont want to have to insert the jQuery into the iframe body, just have it present in the parent body.

Can anyone think of how I might go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁以何悠 2024-12-23 13:13:52

试试这个:

<!--We'll be selecting our images from this frame-->
<iframe id="img_from" src="path/to/same_origin_script.html"></iframe>
<!--We'll be placing the image from the above frame here when the user clicks on it-->
<span id="img_to"></span>
<script type="text/javascript">
    window.onload = function() 
    {
        //The iframe document
        var f = document.getElementById('img_from').contentWindow.document;
        //Register and add event listeners for each image in the iframe... You might want to narrow this down by using class name instead.
        for (var i=0; i<f.getElementsByTagName('img').length; i++)
        {
            //To ensure that event listener is assigned appropriately
            (function(i)
            {
                //Register an image from the iframe.
                var img = f.getElementsByTagName('img').item(i);
                //Add onclick event listener to the image that pulls it from the iframe and puts it in the parent document
                img.onclick = function()
                {
                    //Create a new image element
                    var newImg = document.createElement('img');
                    //Give the new image a meaningful id
                    newImg.id = 'img_to-'+i;
                    //Assign source of clicked image to new image. You may want to use absolute linking in your iframe to avoid bad source links in the parent document.
                    newImg.src = img.src;
                    //Add the new image to the #image_to span in the parent document
                    document.getElementById('img_to').appendChild(newImg);
                    //Optional: remove the image from the iframe's DOM
                    img.parentNode.removeChild(img);
                    //Just in case the image is wrapped in a link, return false to avoid jumping to the href.
                    return false;
                }
                //Still making sure i behaves as expected...
            }) (i);
        }
    }
</script>

iframe 文档中不需要任何脚本,使用 jQuery 的 $(selector).each() 功能绝对可以提高效率。希望这有帮助!

Try this:

<!--We'll be selecting our images from this frame-->
<iframe id="img_from" src="path/to/same_origin_script.html"></iframe>
<!--We'll be placing the image from the above frame here when the user clicks on it-->
<span id="img_to"></span>
<script type="text/javascript">
    window.onload = function() 
    {
        //The iframe document
        var f = document.getElementById('img_from').contentWindow.document;
        //Register and add event listeners for each image in the iframe... You might want to narrow this down by using class name instead.
        for (var i=0; i<f.getElementsByTagName('img').length; i++)
        {
            //To ensure that event listener is assigned appropriately
            (function(i)
            {
                //Register an image from the iframe.
                var img = f.getElementsByTagName('img').item(i);
                //Add onclick event listener to the image that pulls it from the iframe and puts it in the parent document
                img.onclick = function()
                {
                    //Create a new image element
                    var newImg = document.createElement('img');
                    //Give the new image a meaningful id
                    newImg.id = 'img_to-'+i;
                    //Assign source of clicked image to new image. You may want to use absolute linking in your iframe to avoid bad source links in the parent document.
                    newImg.src = img.src;
                    //Add the new image to the #image_to span in the parent document
                    document.getElementById('img_to').appendChild(newImg);
                    //Optional: remove the image from the iframe's DOM
                    img.parentNode.removeChild(img);
                    //Just in case the image is wrapped in a link, return false to avoid jumping to the href.
                    return false;
                }
                //Still making sure i behaves as expected...
            }) (i);
        }
    }
</script>

No script necessary in the iframe document, could definitely be made more efficient with jQuery's $(selector).each() functionality. Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文