在Jquery中显示存储在数组中的列表 [英] Displaying a list stored in an array in Jquery

查看:91
本文介绍了在Jquery中显示存储在数组中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个包含'li'的数组,我如何在正文中显示它?

解决方案

步骤1: 首先你在体内保留一个empt div。



 < ;   div     id  < span class =code-keyword> =  MyList    style   =  display:none >  
< / div >





第2步:





您必须在脚本标签内使用任何事件。我的意思是,像更改,点击或上传或文档。已经完成了一些事件。通过你的阵列进行监控。

  var  html = ' < ul>'; 
for var item in YourArray)
{

html + = item; // 如果有< li>在里面,否则使用html + ='< ul>< li>' + item +< / li>< / ul> ;;

}

html + = ' < / li>< / ul>'

var myDiv = document .getElementById(' MyList');
myDiv.innerHtml = html;
myDiv.style.display = ' block';





现在,如果你想在没有数组的情况下遍历ul的列表,那么上面的for循环应该用'each'条件替换。



  var  html = ' '; 
喜欢:


' ul> li')。each( function (){

html + = this ;

});


' # 。MYLIST')文本(HTML);


If I have an array containing 'li', how do I display it in the body?

解决方案

Step 1: First you keep a empt div inside body.

<div id="MyList" style="display:none">
</div>



Step 2:


You must be using any event inside your script tag. I mean, Like change, click or onload or document.ready some event. Itarate through your array.

var html = '<ul>';
for(var item in YourArray)
{

html += item;  // If it has <li> inside, otherwise use html += '<ul><li>' + item + </li></ul>;

}

html += '</li></ul>'

var myDiv = document.getElementById('MyList');
myDiv.innerHtml = html;
myDiv.style.display = 'block';



Now if you want to iterate through a list of li in ul without array, the above for loop should be replaced with a 'each' condition.

var html = '';
Like :


('ul > li').each(function(){ html += this; });


('#MyList').text(html);


这篇关于在Jquery中显示存储在数组中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆