2014-02-13 48 views
0

我有一塊jQuery,當我重新加載/刷新頁面時,它改變了一個名爲#myQuote的div中的文本。我也想在旁邊有一個鏈接,你可以按下來刷新文本。我已經調用了我想要的link id =「requote」,所以當它被點擊時,它會改變類似於頁面刷新時的文本。點擊jQuery refesh

這裏是jQuery的我到目前爲止:

var myQuotes = new Array(); 
myQuotes[0] = "Quote One"; 
myQuotes[1] = "Quote Two"; 
myQuotes[2] = "Quote Three"; 
myQuotes[3] = "Quote Four"; 

var myRandom = Math.floor(Math.random()*myQuotes.length); 

$('#myQuote').html(myQuotes[myRandom]); 

謝謝:)

+0

嘗試',而不是'.text' .html' – GroundZero

回答

0
var link = $("#requote"); 
link.on("click", function(){ 
    var random = Math.floor(Math.random()*myQuotes.length); 
    $("some.div").html(myQuotes[random]); 
    // prevent default behaviour 
    return false; 
}); 
+0

第一次幸運。非常感謝! – andy88jones

0
$("#requote").click(function(){ 
    var myQuotes = new Array(); 
    myQuotes[0] = "Quote One"; 
    myQuotes[1] = "Quote Two"; 
    myQuotes[2] = "Quote Three"; 
    myQuotes[3] = "Quote Four"; 

    var myRandom = Math.floor(Math.random()*myQuotes.length); 

    $('#myQuote').html(myQuotes[myRandom]); 
}); 
0
$("#requote").on("click", function() { 
    //helper code 
    $('#myQuote').text(<your string>); 
}); 
0
$('#reqoute').click(function(){ 
$('#myQuote').html(myQuotes[myRandom]); 
}); 

嘗試。

0

在此之後

$('#myQuote').html(myQuotes[myRandom]); 

$(document).on('click', '#requote', function() { 
     $('#myQuote').html(myQuotes[myRandom]); 
}); 
0
<script type="text/javascript"> 

$(document).ready(function() { 

    var myQuotes = new Array(); 
    myQuotes[0] = "Quote One"; 
    myQuotes[1] = "Quote Two"; 
    myQuotes[2] = "Quote Three"; 
    myQuotes[3] = "Quote Four"; 

    //var myRandom = Math.floor(Math.random()*myQuotes.length); 

    var myRandom = myQuotes[Math.floor(Math.random()*myQuotes.length)]; 
    //console.log(myRandom); 

    $('#myQuote').text(myRandom); 

    $('#refresh_link').click(function() 
    { 
     var myRandom = myQuotes[Math.floor(Math.random()*myQuotes.length)]; 

     $('#myQuote').text(myRandom); 

    }); 

}); 


</script> 


<div id='myQuote'></div><a id ="refresh_link" href="#">click to refresh</a>