jQuery刷新onclick

时间:2014-02-13 09:26:08

标签: javascript jquery html

我有一段jQuery,当我重新加载/刷新页面时,它会更改名为#myQuote的div中的文本。我还想在它旁边有一个链接,你可以按来刷新文本。我已经调用了链接id =“requote”我想要它,所以当单击它时,它会更改文本,类似于刷新页面时。

这是我到目前为止的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]);

谢谢:)

6 个答案:

答案 0 :(得分: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;
});

答案 1 :(得分: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]);
});

答案 2 :(得分:0)

$("#requote").on("click", function() {
  //helper code
  $('#myQuote').text(<your string>);
});

答案 3 :(得分:0)

$('#reqoute').click(function(){
 $('#myQuote').html(myQuotes[myRandom]);
});

试试这个。

答案 4 :(得分:0)

在此之后

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

 $(document).on('click', '#requote', function() {
      $('#myQuote').html(myQuotes[myRandom]);
 });

答案 5 :(得分: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>