我使用以下问题中的代码将 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 更容易:据我所知,它没有),对此我只能道歉。我会为Meyer、Molly和Cederholm做出必要的牺牲。