参考:
alert(Math.floor(Math.random() * 100) + 1);floor:取整
random:0~1

解决方案 »

  1.   

    <script type="text/javascript">
    alert(Math.floor(Math.random() * 100) + 1);
    alert(Math.floor(Math.random() * 100) + 1);
    document.write("<iframe id="page4" name="page2" src=""article"+(Math.floor(Math.random() * 100) + 1);"4.asp"" style="display:none">文章4</iframe>");
    </script>咋不行呢
      

  2.   

    囧,,,你得注意字符串中引号的嵌套。
    var index = (Math.floor(Math.random() * 100) + 1);
    var str = '<iframe id="page{0}" name="page{0}" src="article{0}.asp" style="display:none">文章{0}</iframe>';
    document.write(str.replace(/\{0\}/g, index));
      

  3.   


    非常感谢你的代码,确实可行,但是我如果需要同时显示3个Iframe就不行了,随机数都是一样的,出来的页面也是一样<script type="text/javascript">
    var index = (Math.floor(Math.random() * 100) + 1);var str1 = '<iframe id="page{0}" name="page{0}" src="http://www.xsafe.cn/article/{0}.htm"></iframe>';
    document.write(str1.replace(/\{0\}/g, index));var str2 = '<iframe id="page{0}" name="page{0}" src="http://www.xsafe.cn/article/{0}.htm"></iframe>';
    document.write(str2.replace(/\{0\}/g, index));var str3 = '<iframe id="page{0}" name="page{0}" src="http://www.xsafe.cn/article/{0}.htm"></iframe>';
    document.write(str3.replace(/\{0\}/g, index));
    </script>
      

  4.   

    你得学会举一反三:var str = '<iframe id="page{0}" name="page{0}" src="http://www.xsafe.cn/article/{0}.htm"></iframe>';
    document.write(str.replace(/\{0\}/g, Math.floor(Math.random() * 100) + 1));
    document.write(str.replace(/\{0\}/g, Math.floor(Math.random() * 100) + 1));
    document.write(str.replace(/\{0\}/g, Math.floor(Math.random() * 100) + 1));
    排除重复,相对复杂一些。var count = 100; // 总数
    var array = new Array(count);
    for (var i = 0; i < count; i++) array[i] = i + 1; // 顺序排列的数组
    var str = '<iframe id="page{0}" name="page{0}" src="http://www.xsafe.cn/article/{0}.htm"></iframe>';
    for (var i = 0; i < 3; i++) {
    var j = Math.floor(Math.random() * (count - i)) + 1; // 从数组中随机抽取一个元素
    var t = array[j];
    array[j] = array[count - 1]; // 将抽取到的元素放到最后
    array[count - 1] = t;
    document.write(str.replace(/\{0\}/g, t));
    }
      

  5.   

    修正一下,处理下标的代码:
    <script type="text/javascript">
    var count = 100; // 总数
    var array = new Array(count);
    for (var i = 0; i < count; i++) array[i] = i + 1; // 顺序排列的数组
    var str = '<iframe id="page{0}" name="page{0}" src="http://www.xsafe.cn/article/{0}.htm"></iframe>';
    for (var i = 0; i < 3; i++) {
    var j = Math.floor(Math.random() * (count - i));
    var t = array[j];
    array[j] = array[count - i - 1];
    array[count - i - 1] = t; // 将抽取到的元素放到最后
    document.write(str.replace(/\{0\}/g, t));
    }</script>