我试图在点击链接时更改iframe的src值,并使用href更改值。单击该按钮时,我会阻止默认操作,并将src值替换为url。该代码适用于不是网址的值,导致iframe显示未找到的网页,但在提供有效网址时不起作用。以下是正在使用的页面的代码。
这是我用来运行网站的主要脚本
$.ajaxSetup ({
cache: false
});
$(document).ready(function(){
$('.pageContent').load('home.php');
setInterval("changePage()",250);
});
function changePage(hash)
{
$('.youtubeLink').click(function(e) {
e.preventDefault();
var temp = $(this).attr("href");
//alert (temp);
$('#youtubePlayerFrame').attr('src', temp);
//when using var temp no action occurs
//when testing temp, it does hold the url, if i replace temp with a string such as
//'asdasd' the script works correctly but replaces the frame with a 404 error
})
$(window).hashchange( function(){
if(!hash) hash=window.location.hash;
hash = hash.replace('#','');
if (hash =='')
{
$('.pageContent').load('home.php');
}
}
else
{
$('.pageContent').load( hash + '.php');
hash = null;
}
})
}
此代码是包含youtube播放器的主要div
<?php
echo '
<div class="youtubePlayer" >
<iframe width="420" height="315" id = "youtubePlayerFrame" src="http://www.youtube.com/embed/j8Szl_JyCUQ" frameborder="0" allowfullscreen style="margin-top:34px; margin-left:20px;"></iframe>
</div>
';
include 'sideDirectory.php';
include 'sideMenu.php';
?>
最后一个代码是带有链接
的目录的php文件<?php
echo'
<div class="sideDirectory">
<table><tbody>
';
$file = fopen("../data/recentlyAdded.txt","r");
$linksFile = fopen("../data/links.txt","r");
while($line =fgets($file))
{
$url = fgets($linksFile);
// echo '<tr><td>' .$url. '</td></tr>';
echo '<tr><td><a class="youtubeLink" href="'.$url.'">'.$line.'</a></td></tr>';
}
fclose($file);
fclose($linksFile);
echo '
</tbody> </table>
</div>
';
?>
答案 0 :(得分:0)
尝试更像这样:
$(document).ready(function(){
$.ajaxSetup ({
cache: false
});
$('.pageContent').load('home.php')
.on('click', '.youtubeLink', function(e) {
e.preventDefault();
$('#youtubePlayerFrame').prop('src', this.href);
});
$(window).on('hashchange', function(){
var hash = window.location.hash.replace('#','');
$('.pageContent').load( (hash.length ? hash : 'home') + '.php');
});
});
绑定事件处理程序在每秒运行四次的间隔中确实没有位置,它们只需要绑定一次即可工作。