2012-01-13 41 views
2

根据引荐来源网址加载特定内容ID这里是我的代码示例,当用户click有链接时,它将加载特定内容id,并且如果有no click动作将显示default内容为#content div如何使用.load()

$(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

所以,如果用户类型或使用直接链接我想剧本究竟做这样.live('click')并加载current id示例为5

让我知道。

+1

以下是如何使用JavaScript获取'GET'参数:http://stackoverflow.com/questions/1403888/get-url -parameter-WI th-jquery - 你可以检查他们的负载,如果他们被设置,然后决定做什么。也许你可以将点击函数外包给一个带有一个参数的命名函数,然后在设置GET参数或点击时调用它。 – Quasdunk 2012-01-13 13:25:18

+0

你试过在你的查询字符串中命名id参数吗? '$('#content')。load(“content.php?id =”+ id);' – jrummell 2012-01-13 13:28:42

回答

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

不过,如果你正在寻找一个无缝(和搜索引擎优化可接受)的内容页面,我会着眼于使用hashbang语法:What's the shebang/hashbang (#!) in Facebook and new Twitter URLs for?

相关问题