0

我使用以下问题中的代码将 YouTube 视频插入页面:如何将 YouTube 播放器的大小从缩略图大小调整为“正常”大小

给定以下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 应该删除.placeholder包含 YouTube 视频的 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

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


更新:

到目前为止,我没有比以前更进一步,当前页面在这里结束,该页面的代码如下:

    <?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做出必要的牺牲。

4

1 回答 1

0

所以......经过一些工作和大量的工作,以及大量的试验和错误以及对 jQuery 文档和其他 SO 问题的大量参考,以了解这里发生了什么,我拼凑了一种特别肮脏的访问方式兄弟.placeholderdiv。

$(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放大图像然后替换为视频,单击.closediv 将其缩小然后能够点击.png放大并再次插入 YouTube 视频。

我不知道为什么这似乎是个好主意,但确实如此。可悲的是它不起作用,我不知道为什么。我有......预感它可能与 jQuery实时事件/函数/事物有关(我对 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>
于 2009-08-23T13:45:29.947 回答