如何在javascript中刷新Div而不是整个页面?

时间:2010-09-27 05:19:44

标签: javascript jquery ajax html

下面的代码工作正常,但我将它放入DRUPAL页面,我希望它刷新“DIV”而不是整个页面。有人可以帮忙吗?谢谢!

            document.write("<div class='box1'><center><h1>Telling Time Worksheets</h1></center><div class='box_number_holder'>")
var nums = [01,02,03,04,05,06,07,08,09,10,11,12];
var gen_nums = [];

function in_array(array, el) {

   for(var i = 0, j = array.length; i < j; i++) 
       if(array[i] == el) return true;
   return false;

}

function get_rand(array) {
    var rand = array[Math.floor(Math.random()*array.length)];
    if(!in_array(gen_nums, rand)) {
       gen_nums.push(rand); 
       return rand;
    }
    return get_rand(array);
}

for(var i = 0; i < 9; i++) {
    document.write("<div class='box_numbers'><center>What Time is it?" + get_rand(nums) + "</center></div>");
}

谢谢!

1 个答案:

答案 0 :(得分:1)

创建一个循环遍历所有动态创建的div元素的函数:

function refreshDivs() {
   $('.box_numbers > center').each(function() {
       $(this).html('What Time is it? ' + get_rand(nums));
   });
}