php - 如何使用 .load() 根据引用 URL 加载特定内容 ID

标签 php jquery

这是我的代码示例,当用户单击链接时,它将加载特定内容id并显示默认内容如果 #content div

中没有没有点击操作
$(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");
});

问题,当用户使用直接链接或在网址栏中输入时如何自动加载内容:示例http://domain.com/content.php?id=5

因此,如果用户键入或使用直接链接,我希望脚本执行与 .live('click') 完全相同的操作,并加载 当前 id 示例为 5

请告诉我。

最佳答案

使用您的示例,您需要一个 anchor 来绑定(bind)到:

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

关于php - 如何使用 .load() 根据引用 URL 加载特定内容 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8851202/

相关文章:

javascript - 在新插入的 Ajax 内容加载完成后,是否有类似于 `$(window).load();` 的功能用于执行功能?

javascript - 当跨度不包含内容时隐藏父级

php - 如何在 PHP 中实现屏幕抓取工具?

PHP unicode解码

php - 完成 Paypal 购买后关闭主题

php - 如何将 mySQL 转换为 PDO

javascript - 在第一张和最后一张幻灯片上禁用上一个/下一个箭头

java - 如何同时接受 multipart 和 application/x-www-form-urlencoded?

jquery - 将对象的方法绑定(bind)到事件的正确方法

javascript - 在 jQuery 中更改 TD 元素