这是我的代码示例,当用户单击
链接时,它将加载特定内容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/