我能想到几种可能性。
一种是使用 php 文件,该文件在您使用类似参数调用文件时返回特定代码(部分)
<a class="fancybox" href="allCodeContent.php?code=show_code_8" ...
另一个仅使用 jQuery 的方法是用于从文件.load()
中调用特定代码(部分).html
例如,一个名为的文件allCodeContent.html
可能具有以下内容:
<div id="show_code_1">This is code one</div>
<div id="show_code_2">This is code two</div>
<div id="show_code_3">This is code three</div>
<div id="show_code_4">This is code four</div>
... etc
然后,您调用主页中的每个代码部分,例如:
<a class="fancybox" href="#show_code_1">show code one in fancybox</a>
<a class="fancybox" href="#show_code_2">show code two in fancybox</a>
<a class="fancybox" href="#show_code_3">show code three in fancybox</a>
<a class="fancybox" href="#show_code_4">show code four in fancybox</a>
...etc.
您可能需要创建一个占位符容器(在您的主页中)以每次加载代码部分
<div id="ajaxContentPlaceholder" style="display:none;"></div>
然后使用这个脚本:
jQuery(function($) {
$(".fancybox").on("click", function(){
var $code = this.href.split("#");
$("#ajaxContentPlaceholder").load("allCodeContent.html #"+$code[1], function(){
$.fancybox(this);
});
return false;
}); // on
}); // ready
请记住,ajax 调用受同源策略的约束