2012-04-11 20 views
-1

我的HTML如下:选择“礼” - jQuery的

<ul id="ul1"> 
    <li> 
    content1 
    </li> 
</ul> 
<ul id="ul2"> 
    <li> 
    content2 
    </li> 
</ul> 
<ul id="ul3"> 
    <li> 
    content3 
    </li> 
</ul> 

编辑

我需要的内容,这些李时珍在数组我如何能做到这一点jQuery的

array['ul1']='content1' 
array['ul2']='content3' 
+0

请将期望的阵列结构因为你在下面的评论中写了你需要包含ul的信息以及... – Marc 2012-04-11 10:08:59

+1

javascript中的数组不能包含字符串键。你需要叫做“对象”或“哈希”。 – 2012-04-11 10:09:09

+0

@Pozadi如果它是一个散列? – Sreevisakh 2012-04-11 10:14:35

回答

3

,你只需要在jQuery的

var myarray=new Array(); 
$("li").each(function() 
      {  
     var text = $(this).text();//you can also write html() function also...  
     var parentUI = $(this).parent('ul').attr("id"); 
     myarray.push(parentUI + ',' + text); 
    }); 
0123下面写

这意味着你只需要写出Element Selector (“element”)这种结构是有问题

+1

“我需要这些李的内容” – nickf 2012-04-11 10:01:14

+0

但我需要知道他们来自哪个ul – Sreevisakh 2012-04-11 10:02:54

+1

这是一个重要的信息,你应该把在你的问题... – Marc 2012-04-11 10:07:30

5

这里给出的是一个班轮为您服务。它会给你像数组这样["content1", "content2", "content3"]

$('li').map(function() { return $(this).text(); }).get(); 
0

喜欢的东西:

var arr = []; 
$('li').each(function(index) { 
    arr.push($(this).text()); 
}); 
0

试试这个:

textArray = []; 
i = 0 

$("li").each(function() { 
    textArray[i] = $(this).text(); 
    i++; 
}) 
0

可能是这个代码是你在寻找什么:

var result = {}; 
$("li").each(function() { 
    result[$(this).parent().attr('id')] = $(this).text(); 
})