2012-01-13 2 views
2

여기 내 코드의 예입니다. 사용자 click에 링크하면 특정 콘텐츠 id이로드되고 no click 동작이 있으면 default 콘텐츠가 표시됩니다. #content div.load()를 사용하여 리퍼러 URL에 따라 특정 콘텐츠 ID를로드하는 방법

$(document).ready(function() { 
    $.ajaxSetup({ 
     cache: false 
    }); 
    $("a.view").live('click', function (e) { 
     var id = $(this).data("id"); 
     $('#content').load("content.php?" + id); 
     e.preventDefault(); 
    }); 
    $('#content').load("content.php"); 
}); 

질문에, 어떻게 사용자 direct link를 사용하는 경우 자동으로 내용을로드 또는 URL 표시 줄에 입력합니다 : 예 그래서 사용자 유형 또는 내가똑같이하고 스크립트를 원하는 직접 링크를 사용하는 경우 http://domain.com/content.php?id=5

을및로드 current id 예 : 5

알려주세요.

+1

다음은 JavaScript로 'GET' 매개 변수를 가져 오는 방법입니다. http://stackoverflow.com/questions/1403888/get-url -parameter-wi th-jquery -로드가 설정되어 있으면로드를 확인한 다음 수행 할 작업을 결정할 수 있습니다. 어쩌면 클릭 함수를 하나의 매개 변수를 취하는 명명 된 함수에 외주 처리 한 다음 GET 매개 변수가 설정되거나 클릭 될 때 호출 할 수 있습니다. – Quasdunk

+0

쿼리 문자열에서 id 매개 변수의 이름을 지정해 보셨습니까? '$ ('# content'). load ("content.php? id ="+ id);' – jrummell

답변

2

귀하의 예제를 사용하면 바인딩 할 앵커가 필요합니다

<a href="#" data-id="5" class="view">Some Link</a> 

는 다음과 같이 작동합니다 코드는 다음과 같습니다

$(function(){ 
    // bind to current and future anchors with the "view" class applied 
    $('a.view').live('click',function(){ 
    // grab the content and load it in to the container 
    $('#container').load('content.php?id=' + $(this).data('id')); 
    // return false so it doesn't follow the link 
    return false; 
    }); 

    // also load the default content (before any click event) 
    $('#container').load('defaultContent.php'); 
}); 

을하지만, 당신이 원활한 찾는 경우 (및 SEO 허용) 콘텐츠 페이지에서 hashbang 구문을 사용하는 방법을 살펴 보겠습니다. What's the shebang/hashbang (#!) in Facebook and new Twitter URLs for?

관련 문제