我想重新启动我在头部链接的样式和processing.js脚本,以便它们在通过ajax请求引入时正确显示。我看到这个代码需要在ajax请求中的哪个位置,但我不知道如何告诉代码来简单地重新应用脚本。我见过使用getScript()来做这件事的人,但是从我可以告诉的重新加载脚本,而不是简单地告诉它重复或反思。所有的脚本都需要自己重新初始化吗?我发现语法highlighters .highlight()方法,但我还没有得到处理脚本加载。目前,Processing.loadSketchFromSources($('#processing'), ['mysketch.pde']);
不起作用。我正在使用所有库的当前版本。惊讶的是,我还没有找到答案,因为很多人似乎都有同样的问题。谢谢你的帮助!在ajax请求后重新初始化processing.js草图
索引页:
$(document).ready(function() {
// put all your jQuery here.
//Check if url hash value exists (for bookmark)
$.history.init(pageload);
//highlight the selected link
$('a[href=' + document.location.hash + ']').addClass('selected');
//Search for link with REL set to ajax
$('a[rel=ajax]').live("click",function(){
//grab the full url
var hash = this.href;
//remove the # value
hash = hash.replace(/^.*#/, '');
//for back button
$.history.load(hash);
//clear the selected class and add the class class to the selected link
$('a[rel=ajax]').removeClass('selected');
$(this).addClass('selected');
//hide the content and show the progress bar
//$('#content').hide();
$('#loading').show();
//run the ajax
getPage();
//cancel the anchor tag behaviour
return false;
});
});
function pageload(hash) {
//if hash value exists, run the ajax
if (hash) getPage();
}
function getPage() {
//generate the parameter for the php script
var data = 'page=' + encodeURIComponent(document.location.hash);
$.ajax({
url: "loader.php",
type: "GET",
data: data,
cache: false,
success: function (html) {
//hide the progress bar
$('#loading').hide();
//add the content retrieved from ajax and put it in the #content div
$('#content').html(html);
//display the body with fadeIn transition
$('#content').fadeIn('fast');
//reapply styles?
//apply syntax highlighting. this works
SyntaxHighlighter.highlight();
//relaod processing sketch, currently displays nothing
Processing.loadSketchFromSources($('#processing'), ['mysketch.pde']);
}
});
}
这阿贾克斯负载的内容:
<!--ajax'd content-->
<??>
<h2>code</h2>
<pre class="brush: php">
$last_modified = filemtime("header.php");
echo("last modified: ");
echo(date("m.j.y h:ia", $last_modified));
</pre>
<script type="application/processing">
</script>
<canvas data-processing-sources="mysketch.pde" id="processing">
</canvas>
</div>
</body>
</html>
<??>
您现在可以添加您的代码,否则答案将永远不会来到其他地方...... –
借调,仍然:如果您在Ajax请求之前运行了一些JS,然后向DOM添加了某些东西,显然它不会受到先前的代码。 – Viruzzo
所以我想我的问题是我如何使.js再次火灾?我可以自己弄清楚。 – expiredninja