如何删除字符串' undefined'从循环输出?

时间:2015-09-17 09:18:50

标签: javascript json ajax

请帮助解决问题。

页面上的

JSFIDDLE)显示4个新闻容器。但在第一个新闻容器输出字符串' undefined'之前。我需要删除字符串' undefined'

HTML:

<div class="banners__wrap-article" id='sk_wrap_82630'></div>

JS:

xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
        document.getElementById('sk_wrap_82630').innerHTML = '<div class="banners__smi2"> \
            <div class="banners__smi2__header"><span class="banners__smi2__header-text">Новости</span></div> \
            ' + cycleItems() + ' \
            </div> \
            <div class="banners__smi2"> \
                <div class="banners__smi2__header"><span class="banners__smi2__header-text"></span></div> \
            </div>';                
    }
}

function cycleItems(){
    var items;
    var massive = JSON.parse(xmlhttp.responseText);

    for (var i = 0; i <= 3; i++) { 
        items = items + constructItem(massive.news[i].url, massive.news[i].img, massive.news[i].title) 
    };

    return items;
}

function constructItem(url, img, title){
    var item;

    return '<div class="banners__smi2__item"> \
                <a href="' + url + '" class="banners__smi2__link"> \
                    <img class="banners__smi2__img" src="' + img + '" alt="" /> \
                    <span class="banners__smi2__title">' + title + '</span> \
                </a> \
            </div>';
}

1 个答案:

答案 0 :(得分:3)

只需将变量<div data-options="dxView : { name: 'graph', title: 'graph' } " > <div data-options="dxContent : { targetPlaceholder: 'content' } "> <h1 class="headline">Dashboard</h1> <div id="chart"></div> </div> </div> 初始化为函数items中的空字符串即可。喜欢

cycleItems()

检查这个jsfiddle。

JSFiddle