我想重新啓動我在頭部鏈接的樣式和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