我正在使用脚本在我的博客 ( https://github.com/swook/jquery-ajaxify-plugin ) 上使用 AJAX,因此用户可以在浏览不同页面时通过音乐播放器收听音乐而不会中断。
除随机按钮外,一切正常。我想对随机按钮进行 AJAX 化。
问题是当我点击链接时,页面会重新加载。
您可以在此处检查不同的问题:http: //www.julienlussiez.com/p/test_20.html(有 3 个具有不同脚本的随机按钮)
测试#1:我有这个[我在这段代码上尝试了很多东西,但似乎没有任何效果]:
<div id="myLuckyPost"><a href="#random" onclick="feelingLucky();return false;" title="Aléatoire">Aléatoire</a></div>
<div id='head'></div>
<script type='text/javascript'>
//<![CDATA[
function showLucky(root){
var feed = root.feed;
var entries = feed.entry || [];
var entry = feed.entry[0];
for (var j = 0; j < entry.link.length; ++j) {
if (entry.link[j].rel == "alternate") {
window.location = entry.link[j].href;
}
}
}
function fetchLuck(luck){
script = document.createElement('script');
script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
script.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(script);
}
function readLucky(root){
var feed = root.feed;
var total = parseInt(feed.openSearch$totalResults.$t,10);
var luckyNumber = Math.floor(Math.random()*total);
luckyNumber++;
fetchLuck(luckyNumber);
}
function feelingLucky(e){
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = '/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky';
document.getElementsByTagName('head')[0].appendChild(script);
if(e && e.stopPropagation) {
e.stopPropagation();
} else {
e = window.event;
e.cancelBubble = true;
}
return false;
}
//]]>
</script>
测试#2:使用另一个脚本来随机化文章。问题还是一样。
<script>
<!--
/*
Random link button- By JavaScript Kit (http://javascriptkit.com)
Over 300+ free scripts!
This credit MUST stay intact for use
*/
//specify random links below. You can have as many as you want
var randomlinks=new Array()
randomlinks[0]="http://www.julienlussiez.com/2013/01/le-repos-du-fou_21.html"
randomlinks[1]="http://www.julienlussiez.com/2012/11/dissonance-3.html"
randomlinks[2]="http://www.julienlussiez.com/2012/10/renaitre.html"
randomlinks[3]="http://www.julienlussiez.com/2013/01/defaillance.html"
randomlinks[4]="http://www.julienlussiez.com/2012/08/fragile-2012.html"
function randomlink(){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
}
//-->
</script>
<form>
<p><input type="button" name="B1" value="Aléatoire" onclick="randomlink(); return false;" /></p> </form>
测试#3:使用另一个脚本来随机化文章。问题还是一样。
<script>
/* Simple Random Posts function for Blogger
* Copyright 2012 Yu-Jie Lin
* Licensed under the MIT License
*/
function blogger_random_post(){
// You can replace the value with your blog ID
var blogID = $('#blogID').val();
// for example:
// blogID = "4586487713356156869";
var BLOGGER_POSTS_API = "http://www.julienlussiez.com/feeds/posts/summary";
// appending necessary parameters
BLOGGER_POSTS_API += "?alt=json-in-script&max-results=1&callback=?";
// replacing blogID with value
BLOGGER_POSTS_API = BLOGGER_POSTS_API.replace('blogID', blogID.toString());
$('#btn-blogger-random').text('Tossing dice...');
// retrieving posts count
$.getJSON(BLOGGER_POSTS_API, function(data){
var posts_count = parseInt(data.feed.openSearch$totalResults.$t);
// index is 1-based
var index = Math.floor(Math.random() * posts_count) + 1;
// retrieving post link
$.getJSON(BLOGGER_POSTS_API + '&start-index=' + index, function(data){
$.each(data.feed.entry[0].link, function(idx, link){
if (link.rel == 'alternate')
// redirecting
document.location.href = link.href;
});
});
});
}
$(function(){
$('#btn-blogger-random').click(blogger_random_post);
});
</script>
<label>Blog ID <input id="blogID" value="4586487713356156869"/></label>
<button id="btn-blogger-random">I'm Feeling Lucky »</button>
谢谢!如果你有一些线索,那就太好了!