代码之家  ›  专栏  ›  技术社区  ›  David Thomas

删除YouTube flash视频播放器容器/设置其动画时出现问题

  •  0
  • David Thomas  · 技术社区  · 15 年前

    我使用以下问题中的代码将YouTube视频插入页面: How to resize a YouTube player, from thumbnail size to 'normal' size .

    给定以下html:

    <dl>
    <dt>Thoughts of Sacrament</dt>
    <dd><div class="placeholder"><img src="img/H5ZEYFgmfYo.png" id="H5ZEYFgmfYo" /></div><div class="close">x</div><p>There is no purpose to this text, it's just here in order to provide a frame of visual reference while I work upon the code behind this here 'page,' still: I hope you enjoy, uh...looking.</p></dd>
    <dt>Sanity falling</dt>
    <dd><img src="img/2ieLb3RAblA.png" id="2ieLb3RAblA" /></dd>
    </dl>
    

    .placeholder 包含YouTube视频的div:

    $(document).ready(function(){
    
        // ...Other code to insert the YouTube...
    
    $("div.close").click(function() {
    
        $(this).sibling("div").remove();
                // $(this).parent().empty();
    
    });
    

    });

    .png ,但我在访问相关div时遇到问题。注释掉的代码可以工作,并清空父元素,但我似乎无法访问同级元素 .占位符

    任何帮助都将不胜感激,提前感谢=)


    更新:

    到目前为止,我没有比以前更进一步,当前页面已经结束 here ,该页的代码如下所示:

        <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
        <title>jQuery & YouTube</title>
        <link rel="stylesheet" type="text/css" href="css/demo.css" />
    
        <script type="text/javascript" src="js/jquery.js"></script>
    
    <!--
    
        Currently -almost- works
            - unfortunately the 'close' option doesn't care
            if the video's been played or not (and will remove the picture if it's there).
            - the close button also (tries?) to animate the flash object and then drops
            the replacement image in. Looks wrong.
    
            On updating the page, the image -after closing/removing the video
            is no longer 'clickable'
    
    -->
    
            <script type="text/javascript">
    $(document).ready(function(){
    
        $(".placeholder img").click(function () {
    
            var videoID = $(this).attr("id");
    
            $(this).animate({ 
            width: "445px",
            height: "364px"
            }, 600, function() {
    
                $(this).replaceWith("<object id=\"" + videoID + "\" class=\"yt\" width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + videoID + "&hl=en&fs=1&color1=0x3a3a3a&color2=0x999999\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"autoplay\" value=\"1\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/" + videoID + "&hl=en&fs=1&color1=0x3a3a3a&color2=0x999999\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>", function() {$(this).parent().children().find("div .close").toggle()} );
            });
        });
    
        $("div.close").click(function() {
    
            var videoID = $(this).parent().find("object").attr("id");
    
            $(this).parent().children("div .placeholder").animate({
                width: "252px",
                height: "199px"
                }, 600, function() {
    
                    $(this).replaceWith("<div class=\"placeholder\"><img id=\"" + videoID + "\" src=\"img/" + videoID + ".png\" /></div>");
                    });
            });
    
    });
    
            </script>
    </head>
    <body>
    <div id="wrap">
    
        <div id="head">
    
            <div id="branding" title="demo page for jQuery issues.">
    
                <h1>jQuery and YouTube problems</h1>
    
                <div id="navigation">
    
                    <ul>
                        <li><a href="#">home</a></li>
                        <li><a href="#">videos</a></li>
                        <li><a href="#">dates</a></li>
                    </ul>
                </div>
            </div>
        </div>
        <div id="content">
    
        <dl>
            <dt>Thoughts of Sacrament</dt>
            <dd>
                <div class="placeholder">
                    <img src="img/H5ZEYFgmfYo.png" id="H5ZEYFgmfYo" />
                </div>
    
                <div class="close">x</div>
    
                <p>There is no purpose to this text, it's just here in order to provide a frame of visual reference while I work upon the code behind this here 'page,' still: I hope you enjoy, uh...looking.</p>
            </dd>
    
        <dt>Sanity falling</dt>
            <dd>
                <div class="placeholder">
                    <img src="img/2ieLb3RAblA.png" id="2ieLb3RAblA" />
                </div>
    
                <div class="close">x</div>
            </dd>
        </dl>
        </div>
    </div>
    </body>
    </html>
    

    Meyer Molly Cederholm

    1 回复  |  直到 7 年前
        1
  •  0
  •   David Thomas    15 年前

    所以…经过一些工作,很多很多很多,嗯, 太多了 经过反复试验和大量参考jQuery文档以及其他问题,为了了解这里发生了什么,我拼凑了一种特别肮脏的访问兄弟姐妹的方法 .placeholder 部门。

    $(document).ready(function(){
    
    // ...Other code to insert the YouTube...
    
        $("div.close").click(function() {
    
            var videoID = $(this).parent().find("object").attr("id");
    
            $(this).parent().children("div .placeholder").replaceWith("<div class=\"placeholder\"><img id=\"" + videoID + "\" src=\"img/" + videoID + ".png\" /></div>");
    
        });
    
    });
    
    <div id="content">
    
    <dl>
        <dt>Thoughts of Sacrament</dt>
        <dd>
                <div class="placeholder"><img src="img/H5ZEYFgmfYo.png" id="H5ZEYFgmfYo" /></div>
                <div class="close">x</div>
                <p>There is no purpose to this text, it's just here in order to provide a frame of visual reference while I work upon the code behind this here 'page,' still: I hope you enjoy, uh...looking.</p>
            </dd>
    
        <dt>Sanity falling</dt>
        <dd>
                <img src="img/2ieLb3RAblA.png" id="2ieLb3RAblA" />
            </dd>
    </dl>
    
    </div>
    

    我曾希望这会以某种方式将页面重置为其起始位置,以便我可以单击 .png 要放大图像,然后替换为视频,请单击 .close .png 将其放大并再次插入YouTube视频。

    我不知道为什么这看起来是个好主意,但确实如此。遗憾的是,它不起作用,我也不知道为什么。我有…一个 预感 这可能与jQuery有关 live 事件/功能/事物(我是 不熟悉jQuery),但我还不知道如何将所有内容连接在一起。

    为了完整起见,由于这是对前面问题的发展,我将从 html 下面的文件,所以任何进来的人都可以看到我做了什么,如果不能更好地理解我的决策过程,希望能知道为什么。

    整个代码:

    <?xml version="1.0" encoding="utf-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
        <title>in the absence of light | ITAOL</title>
        <link rel="stylesheet" type="text/css" href="css/stylesheet.css" />
    
        <script type="text/javascript" src="js/jquery.js"></script>
    
            <script type="text/javascript">
    $(document).ready(function(){
    
        $("img").click(function () {
    
            var videoID = $(this).attr("id");
    
            $(this).animate({ 
            width: "445px",
            height: "364px"
            }, 600, function() {
    
                $(this).replaceWith("<object id=\"" + videoID + "\" class=\"yt\" width=\"425\" height=\"344\"><param name=\"movie\" value=\"http://www.youtube.com/v/" + videoID + "&hl=en&fs=1&color1=0x3a3a3a&color2=0x999999\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"autoplay\" value=\"1\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/v/" + videoID + "&hl=en&fs=1&color1=0x3a3a3a&color2=0x999999\" type=\"application/x-shockwave-flash\" allowscriptaccess=\"always\" allowfullscreen=\"true\" width=\"425\" height=\"344\"></embed></object>");
            });
        });
    
        $("div.close").click(function() {
    
            var videoID = $(this).parent().find("object").attr("id");
    
            $(this).parent().children("div .placeholder").replaceWith("<div class=\"placeholder\"><img id=\"" + videoID + "\" src=\"img/" + videoID + ".png\" /></div>");
    
        });
    
    });
    
            </script>
    
    </head>
    
    <body>
    
    <div id="wrap">
    
        <div id="head">
    
            <div id="branding" title="In the Absence of Light.">
    
                <h1 title="In the Absence of Light">In The Absence of Light</h1>
    
                <div id="navigation">
    
                    <ul>
                        <li><a href="#">home</a></li>
                        <li><a href="#">videos</a></li>
                        <li><a href="#">dates</a></li>
                    </ul>
    
                </div>
    
            </div>
    
        </div>
    
        <div id="content">
    
        <dl>
            <dt>Thoughts of Sacrament</dt>
            <dd>
                <div class="placeholder">
                    <img src="img/H5ZEYFgmfYo.png" id="H5ZEYFgmfYo" />
                </div>
    
                <div class="close">x</div>
    
                <p>There is no purpose to this text, it's just here in order to provide a frame of visual reference while I work upon the code behind this here 'page,' still: I hope you enjoy, uh...looking.</p>
            </dd>
    
        <dt>Sanity falling</dt>
            <dd>
                <div class="placeholder">
                    <img src="img/2ieLb3RAblA.png" id="2ieLb3RAblA" />
                </div>
    
                <div class="close">x</div>
            </dd>
    
        </dl>
    
        </div>
    
        <div id="foot">
    
        </div>
    
    
    </div>
    
    </body>
    
    </html>