刷新div标签而不刷新a

时间:2015-01-08 05:58:04

标签: javascript jquery ajax

我想在点击事件中重新加载div标签。我想在点击事件中生成随机密码。现在整个页面都刷新了。这需要一些时间。所以我想每隔3秒或点击事件刷新div标签。我附上了我的代码。

 <body>
 hello 
 <div class="refresh">
   <?php
      function randomPassword() {
          $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
          $pass = array(); //remember to declare $pass as an array
          $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
          for ($i = 0; $i < 8; $i++) {
              $n = rand(0, $alphaLength);
              $pass[] = $alphabet[$n];
          }
          return implode($pass); //turn the array into a string
      }
      echo $pwd=randomPassword();
   ?>

 </div>
 <button class="click">click me</button>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
 <script>
     $(document).ready(function(){
         $(".click").click(function(){
             location.reload();
         });
     });
</script>
</body> 

3 个答案:

答案 0 :(得分:1)

单击按钮,您将使用location.reload();重新加载页面。仅刷新div,您需要修改代码。

<body>
hello 
 <div class="refresh">

 </div>
 <button class="click">click me</button>
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script>
  $(document).ready(function(){
  $(".click").click(function(){
  var str = randomPassword();
  $(".refresh").html(str);
  });

function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
$pass = array(); //remember to declare $pass as an array
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
for ($i = 0; $i < 8; $i++) {
    $n = rand(0, $alphaLength);
    $pass[] = $alphabet[$n];
      }
    return implode($pass); //turn the array into a string
    }
 });
</script>
</body> 

答案 1 :(得分:1)

                <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
                <html>
                 <head>
                  <title> New Document </title>
                   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
                 <script>
                 setInterval(RandPwd,30000);
                 function RandPwd()
                {
                    var pwd = "";
                    var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

                    for( var i=0; i < 5; i++ )
                        pwd += possible.charAt(Math.floor(Math.random() * possible.length));

                    $(".refresh").html(pwd);
                return false;
                }
                 </script>
                 </head>

                 <body>
                  hello 
                 <div class="refresh">
                 </div>
                 <button class="click" onclick="return RandPwd();">click me</button>

                 </body>
                </html>

答案 2 :(得分:0)

这是为了刷新div标签而不重新加载页面。

 <script>
 var auto_refresh = setInterval(function () {
$('.refresh').fadeOut('slow', function() {
    $(this).load('/echo/json/', function() {
        $(this).fadeIn('slow');
    });
});
}, 3000);
</script>