【问题标题】:Problem with removing/animating a YouTube flash video-player container删除/动画 YouTube Flash 视频播放器容器的问题
【发布时间】:2009-08-23 11:55:17
【问题描述】:

我使用以下问题中的代码将 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>

我预计以下 jQuery 应该删除包含 YouTube 视频的 .placeholder div:

$(document).ready(function(){

    // ...Other code to insert the YouTube...

$("div.close").click(function() {

    $(this).sibling("div").remove();
            // $(this).parent().empty();

});

});

最终我的目标是“反转”原始动画,然后将 youtube 视频换成原始 .png,但我无法访问相关的 div。注释掉的代码有效,并清空了父元素,但我似乎无法访问兄弟.placeholder

任何帮助将不胜感激,在此先感谢 =)


更新:

到目前为止,我没有比以前更进一步,当前页面已结束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>

你会注意到 div 的过度使用(使用是因为我认为 - 出于某种原因 - 它可能会使导航 DOM 更容易:据我所知,它没有),对此我只能道歉.我会为MeyerMollyCederholm做出必要的牺牲。

【问题讨论】:

    标签: jquery youtube jquery-1.3 flash


    【解决方案1】:

    所以...经过一些工作和很多很多,以及,很多 的反复试验以及对 jQuery 文档和其他 SO 问题的大量参考,以了解这里发生了什么,我一起破解了一个访问兄弟.placeholder div的特别肮脏的方法。

    $(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 div将其缩小,然后可以单击.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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-12
      • 2014-05-01
      • 2021-09-17
      • 1970-01-01
      • 2016-03-24
      • 2014-12-21
      相关资源
      最近更新 更多