我正在为我的博客使用此代码片段插件:http ://www.steamdev.com/snippet/ ,但该插件在页面加载时不起作用。它仅在第一页刷新时有效。
我使用 jquery.ajax 请求将我的内容加载到特定的 div 中,我正在尝试这个:
$(window).on("load", function(){
$("pre.cplus").snippet("cpp",{style:"acid"});
$("pre.php").snippet("php",{style:"acid"});
});
我也尝试触发加载事件,但我不知道它是否正确..
另一个问题:我用 php 字符串构建我的 html,如下例所示:
$string = '<pre class="cplus">
#include <iostream>
int main()
{
//c++ code
}
</pre>
<pre class="php">
<?php
function foo()
{
// PHP code
}
?>
</pre>';
echo $string; // ajax -> success
但 PHP 片段显示为空(c++ 可以)。在我的页面上显示 php 代码片段的任何其他方式(或插件)?谢谢你。
已解决:问题不在于插件或 Iserni 建议。我在页面加载(ajax)时遇到问题。这就是我加载页面的方式:
function pageload(hash) {
if(hash == '' || hash == '#php')
{
getHomePage();
}
if(hash)
{
getPage();
}
}
function getHomePage() {
var hdata = 'page=' + encodeURIComponent("#php");
//alert(hdata);
$.ajax({
url: "homeloader.php",
type: "GET",
data: hdata,
cache: false,
success: function (hhtml) {
$('.loading').hide();
$('#content').html(hhtml);
$('#body').fadeIn('slow');
}
});
}
function getPage() {
var data = 'page=' + encodeURIComponent(document.location.hash);
//alert(data);
$.ajax({
url: "loader.php",
type: "GET",
data: data,
cache: false,
success: function (html) {
$('.loading').hide();
$('#content').html(html);
$('#body').fadeIn('slow');
}
});
}
$(document).ready(function() {
// content
$.history.init(pageload);
$('a[href=' + window.location.hash + ']').addClass('selected');
$('a[rel=ajax]').click(function () {
var hash = this.href;
hash = hash.replace(/^.*#/, '');
$.history.load(hash);
$('a[rel=ajax]').removeClass('selected');
$(this).addClass('selected');
$('#body').hide();
$('.loading').show();
getPage();
return false;
});
// ..... other code for menus, tooltips,etc.
我知道这是实验性的,我已经混合了各种教程,但现在它可以工作了.. 非常感谢评论.. 谢谢大家。