0

リンクがクリックされたときに Iframe の src 値を変更し、そこからの href で値を変更しようとしています。ボタンがクリックされると、デフォルトのアクションを防止し、src 値を URL に置き換えます。このコードは、URL ではない値に対して機能するため、iframe に見つからないページが表示されますが、有効な URL が指定されている場合は機能しません。使用中のページのコードは次のとおりです。

これは、サイトを実行するために使用しているメイン スクリプトです。

$.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>
';
?>
4

1 に答える 1

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');
    });
});

バインド イベント ハンドラーは、1 秒間に 4 回実行される間隔では実際には場所がありません。バインドする必要があるのは 1 回だけです。

于 2013-07-09T18:29:55.980 に答える