【发布时间】:2011-12-01 11:32:11
【问题描述】:
我想重新启动我在头部链接到的样式和 processing.js 脚本,以便它们在通过 ajax 请求引入时正确显示。我看到该代码需要在 ajax 请求中的哪个位置,但我不知道如何告诉代码简单地重新应用脚本。我见过人们使用 getScript() 来执行此操作,但据我所知,它会重新加载脚本,而不是简单地告诉它重复或重新启动。所有脚本都需要自己重新初始化吗?我找到了语法荧光笔 .highlight() 方法,但我还没有加载处理脚本。目前,Processing.loadSketchFromSources($('#processing'), ['mysketch.pde']); 不起作用。我正在使用所有库的当前版本。很惊讶我还没有找到答案,因为很多人似乎有同样的问题。感谢您的帮助!
索引页面:
$(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 加载的内容:
<!--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>
<??>
【问题讨论】:
-
你现在可以添加你的代码,否则答案永远不会出现......
-
第二,仍然:如果你有一些JS在Ajax请求之前运行,然后你向DOM添加一些东西,显然它不会受到前面代码的影响。
-
所以我想我的问题是如何让 .js 再次触发?我自己也许能弄清楚。
标签: javascript ajax jquery processing.js