简体   繁体   English

如何在javascript中显示带有数组项的div

[英]How to display a div with items from array in javascript

I have a param called "exraData" which can be a string or array (depends on some conditions).我有一个名为“exraData”的参数,它可以是字符串或数组(取决于某些条件)。

I have to display some content using java script.我必须使用 java 脚本显示一些内容。

in case "exraData" is a String the content is displayed well by the following code:如果“exraData”是一个字符串,则以下代码可以很好地显示内容:

this.resulto = '<div class="avatarHeight">' +
            '<div class="avatar">' + spanPic + '</div>' +
            '<div class="info ' + company + '">' + item.show +
            '<div class="tt-hint">' + exraData + '</div>' +
            '</div>' +
            '</div>';

In case it is array I use the following code but nothing is displayed:如果它是数组,我使用以下代码但没有显示任何内容:

 this.resulto = '<div class="avatarHeight">' +
                    '<div class="avatar">' + spanPic + '</div>' +
                    '<div class="info ' + company + '">' + item.show
                    for(var i=0;i<exraData.length;i++){
                        +'<div class="tt-hint">' + exraData[i] + '</div>'
                    }
                    +'</div>' +
                    '</div>';

You can't have a for loop inline.你不能有内联的 for 循环。 Here is the for loop outside with the desired result.这是外部的 for 循环,具有所需的结果。

 var spanPic='tempPic'; var company='xyz'; var item={}; item.show='temp'; var exraData=['one','two','three']; this.resulto = '<div class="avatarHeight">' + '<div class="avatar">' + spanPic + '</div>' + '<div class="info ' + company + '">' + item.show; for(i=0;i<exraData.length;i++){ this.resulto +='<div class="tt-hint">' + exraData[i] + '</div>'; } this.resulto+='</div>' + '</div>'; document.write(this.resulto);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM