2013-07-09 6 views
0

링크를 클릭 할 때 Iframe의 src 값을 변경하고 href를 사용하여 값을 변경하려고합니다. 버튼을 클릭하면 기본 동작을 방지하고 src 값을 url로 바꿉니다. 이 코드는 URL이 아닌 값을 사용하여 iframe에서 페이지를 찾을 수 없도록 표시하지만 유효한 URL이 제공되면 작동하지 않습니다. 다음은 사용중인 페이지의 코드입니다.Jquery로 attr 값 변경하기

이 내가 사이트를

$.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; 
} 
}) 
} 

이 코드를 실행하는 데 사용 된 주요 스크립트는이 마지막 코드에 대한 PHP 파일 인 유튜브 플레이어

<?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 
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

신성한 허튼 소리, 매초 네 번씩 새 클릭 이벤트를 바인딩하고 있습니다. 어떻게 그 일을 해나나요? – adeneo

+0

WAAHHHH! 그것은 iframe입니다. 이것은 브라우저 나 다른 무언가의 동일한 출처 정책과 관련이 있을까요? – Gijs

+0

앵커 태그가 * if * target입니까? 'target = "iframeid"' –

답변

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

바인딩 이벤트 핸들러 정말 그들은 단지 일을 한 번에 구속 될 필요가 4 회 두 번째를 실행하는 간격 곳이 없습니다.

+0

코드를 지적하고 계속 학습 해 주셔서 감사합니다. 시행 착오로. 그래도 프레임을 제대로로드하지 못하고 그냥 앉아 있습니다. 누구든지 http://musicsharing.co.nf/pages/home.html을보고 싶다면 헤더의 원 버튼을 클릭하십시오. –

+0

그리고'.youtubelink'와'# youtubePlayerFrame'은 어디에 있나요? 이것들은 당신이 만든 요소들입니다. 그들은 Youtube에 있지 않습니다. – adeneo

+0

해당 디렉토리의 링크가 .youtubelink 클래스 아래에없고 프레임 ID가 #youtubePlayerFrame 인 경우 URL을로드하려고합니다. –