显示一系列可以隐藏/显示的 YouTube 嵌入内容
我的目标是在一个页面上显示一系列嵌入的 YouTube 视频,但允许用户通过单击与页面上特定 YouTube 视频关联的按钮来隐藏或显示任何 YouTube 视频。我制作了一个表单,将 youtube 嵌入代码提交到 mysql 数据库,并创建了一个 php 文件来使用 for 循环检索每个嵌入代码。对于 for 循环的每次迭代,都会弹出一个 YouTube 视频,并带有相应的按钮,该按钮允许用户隐藏或显示 YouTube 视频。
当 mysql 表中只有一个条目时它可以工作,但当上传更多 youtube 嵌入代码时它不起作用。例如,当上传两个 youtube 嵌入代码时,会创建两个按钮,但是当我单击这两个按钮中的任何一个时,它只会显示或隐藏第一个 youtube 嵌入。所有按钮都不会显示第二个 YouTube 视频。
这是经过一些编辑的代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>hide/show iframe</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<style type="text/css">
<!--
#frDocViewer {
width:70%;
height:50%;
display:none;
}
-->
</style>
<script type="text/javascript">
<!--
function HideFrame() {
var fr = document.getElementById ("frDocViewer");
if(fr.style.display=="block") {
fr.style.display="none";
} else {
fr.style.display="block";
}
}
//-->
</script>
</head>
<body>
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$lastid = mysql_query ("SELECT * FROM embed ORDER BY id DESC LIMIT 1");
$lastid = mysql_fetch_assoc($lastid);
$lastid = $lastid['id'];
for ($count=1; $count<= $lastid ; $count++) {
$iframe = mysql_query ("SELECT * FROM embed WHERE id=$count ");
$iframe = mysql_fetch_assoc($iframe);
$iframe = $iframe['url'];
echo "
<image src='utube.gif' onclick='HideFrame()' />
<div id='frDocViewer'>
$iframe
</div>
";
}
?>
</body>
</html>
My goal is to show a series of embedded youtube videos on one page but allowing the user to hide or show any youtube video by clicking a button associated with a specific youtube video on the page. I made a form that submits the youtube embed code to a mysql database and I created a php file to retrieve every embed code using a for loop. For each iteration of the for loop, a youtube video will pop up with a corresponding button which will allow the user to hide or show the youtube video.
It works when there is only one entry in the mysql table but does not work when more youtube embed codes are uploaded. For example, when there are two youtube embed codes uploaded, two buttons are created, but when I click either of the two buttons, it will only show or hide the first youtube embed. None of the buttons will show the second youtube video.
here is the code with some edits:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>hide/show iframe</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"/>
<style type="text/css">
<!--
#frDocViewer {
width:70%;
height:50%;
display:none;
}
-->
</style>
<script type="text/javascript">
<!--
function HideFrame() {
var fr = document.getElementById ("frDocViewer");
if(fr.style.display=="block") {
fr.style.display="none";
} else {
fr.style.display="block";
}
}
//-->
</script>
</head>
<body>
<?php
mysql_connect ("","","") or die(mysql_error());
mysql_select_db ("") or die(mysql_error());
$lastid = mysql_query ("SELECT * FROM embed ORDER BY id DESC LIMIT 1");
$lastid = mysql_fetch_assoc($lastid);
$lastid = $lastid['id'];
for ($count=1; $count<= $lastid ; $count++) {
$iframe = mysql_query ("SELECT * FROM embed WHERE id=$count ");
$iframe = mysql_fetch_assoc($iframe);
$iframe = $iframe['url'];
echo "
<image src='utube.gif' onclick='HideFrame()' />
<div id='frDocViewer'>
$iframe
</div>
";
}
?>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为每个div都有相同的ID。您需要为每个 DIV 创建唯一的 ID 以显示或隐藏即。 frDocViewer1、frDocViewer2 等
使用 $count 变量将其值回显到 ID,因为每次循环迭代它都会增加 1。
然后您只需要确保每个 DIV 都有相应的 Javascript。我将使用您的 onclick 标记将 id 发送到 javascript 函数中。
然后将 javascript 代码设置为如下所示:
然后最后您需要更改 CSS 以使 frDocViewer 成为一个类而不是一个独特的样式。请注意上面的 PHP 回显字符串,我在 DIV 的新类属性中添加了
PS:此代码实际上可能无法编译,它只是一个粗略的指南。
Because each div has the same ID. You need to create unique ID's for each DIV to show or hide ie. frDocViewer1, frDocViewer2 etc
Use your $count variable to echo it's value onto the ID as it will increment by 1 for each iteration of the loop.
Then you just need to make sure that you have corresponding Javascript for each of those DIV's. I would send the id into the javascript function using your onclick tag.
And then have the javascript code as something like:
Then finally you need to change your CSS to make frDocViewer a class rather than a unique style. Notice above in the PHP echo string I added in the new class attribute to the DIV
PS: This code might not actually compile, it's just a rough guide.
我认为问题在于这一行:
ID 在整个页面中应该是唯一的,但是您在循环的每次迭代中都创建了一个具有相同 ID 的新元素。我还没有对此进行测试,但我的假设是,当您的 JavaScript 执行隐藏/显示该 ID 时,它只会拾取它找到的第一个具有该名称的 ID。
您需要更改代码,为每次迭代提供唯一的 ID,并在调用
HideFrame()
函数时传递对该元素的引用。然后该函数可以对该特定 ID 进行操作。I think the problem is with this line:
IDs are supposed to be unique across the entire page, but you're creating a new element with the same ID on each iteration of your loop. I haven't tested this, but my assumption is that when your JavaScript executes to hide/show that ID, it's only picking up the first ID by that name that it finds.
You'll need to change your code to give a unique ID to each iteration, and pass a reference to that element when you call your
HideFrame()
function. The function can then operate on that specific ID.嗯...从哪里开始...
如果您
getElementById
,其中您的 id 是frDocViewer
并且frDocViewer
被每个视频使用,将会有问题。在我看来,像这样的东西怎么样:
根本没有测试过,但是你需要寻找更通用的东西,然后硬编码一个ID......
hmm...where to start....
if you
getElementById
where your id isfrDocViewer
ANDfrDocViewer
is used by every video there will be problems.how about something like
Not at all tested, but you NEED to go for something more GENERIC then hard coding an id....in my opinion