随机图片“undefined”

时间:2013-08-20 11:59:15

标签: javascript random undefined getelementbyid

当我应用以下脚本时,它会在输出中随机显示“undefined”。我已经检查过它应该是由javascript引起的。我可以知道可能是什么错误吗?

<div id="changeImgNews"></div>

<script language=Javascript>
var countNum = <{$block.countNum}>;

var checkRepNews = 1;
var ranNumNews = 0;
var ranContentNews = "";
var arti_name = new Array();
var arti_img = new Array();
var arti_id = new Array();
var arti_model = new Array();
var arti_desc = new Array();

<{foreach from=$block.news item=arti key=count}>
arti_name[<{$count}>] = "<{$arti.name}>";
arti_img[<{$count}>] = "<{$arti.img}>";
arti_id[<{$count}>] = "<{$arti.id}>";
arti_model[<{$count}>] = "<{$arti.model}>";
arti_desc[<{$count}>] = "<{$arti.desc}>";
<{/foreach}>

function randomImageNews()
{
ranNumNews = Math.round(Math.random()*countNum);
while (ranNumNews == checkRepNews){
    ranNumNews = Math.round(Math.random()*countNum+1);
};
checkRepNews = ranNumNews;
ranContentNews = "<a href='<{$portal_url}>/modules/content/item.php?itemid=16' title='"+arti_name[ranNumNews]+"'><img src='<{$portal_url}>/modules/news/images/news"+arti_img[ranNumNews]+"' style='border: 1px solid #000000; ' / width='220' height='220'></a><br /><div style='text-align:left; width:220px;'>"+arti_model[ranNumNews]+" - <a href='<{$portal_url}>/modules/content/item.php?itemid=16'>"+arti_name[ranNumNews]+"</a>";
document.getElementById('changeImgNews').innerHTML = ranContentNews;
setTimeout("randomImageNews();", 8000);
}
randomImageNews();
</script>

2 个答案:

答案 0 :(得分:0)

使用Math.floor(Math.random()*countNum)代替Math.round(Math.random()*countNum+1)

P.S。 看在上帝的份上,重构这个! : - )

答案 1 :(得分:0)

我试图折射代码,但未经过测试。试一试。

<script language=Javascript>
    var randomizer = null,
        randomizerController = null;
        article = {},
        portalUrl = '';

    article = {
        id : [],
        name : [],
        image : [],
        model : [],
        desc : []
    };

    portalUrl = '<{$portal_url}>';

    randomizer = function() {
        var randomIndex = 0,
            content = '';

        randomIndex = Math.floor((Math.random() * article.id.length) + 1);

        content = "<a href='" + portalUrl + "/modules/content/item.php?itemid=16' title='" + article.name[randomIndex] + "'>";
        content += "<img src='" + portalUrl + "/modules/news/images/news" + article.image[randomIndex] +"'";
        content += "style='border: 1px solid #000000; ' / width='220' height='220'></a><br />";
        content += "<div style='text-align:left; width:220px;'>" + article.model[randomIndex];
        content += " - <a href='" + portalUrl + ">/modules/content/item.php?itemid=16'>" + article.name[randomIndex] +"</a>";

        console.log(content);
    };

    <{foreach from=$block.news item=arti key=count}>
        article.id.push('<{$arti.id}>');
        article.name.push('<{$arti.name}>');
        article.image.push('<{$arti.img}>');
        article.model.push('<{$arti.model}>');
        article.desc.push('<{$arti.desc}>');
    <{/foreach}>

    randomizer = setInterval(randomizerController, 8000);

</script>