Jquery each-获取ul中的li元素
我的页面上有以下HTML:Jquery each-获取ul中的li元素,jquery,each,Jquery,Each,我的页面上有以下HTML: <div class="phrase"> <ul class="items"> <li class="agap"><ul><li>TEXT1</li></ul></li> <li class="agap"><ul> </ul></li> <!-- empty ul -->
<div class="phrase">
<ul class="items">
<li class="agap"><ul><li>TEXT1</li></ul></li>
<li class="agap"><ul> </ul></li> <!-- empty ul -->
<li class="aword">TEXT2</li>
..
</ul>
</div>
<div class="phrase"> ... </div>
我目前有以下javascript代码:
<script>
$(function() {
$('.phrase .items').each(function(){
var myText = "";
// At this point I need to loop all li items and get the text inside
// depending on the class attribute
alert(myText);
});
};
</script>
$(函数(){
$('.phrase.items')。每个(函数(){
var myText=“”;
//在这一点上,我需要循环所有的li项,并将文本放在里面
//取决于类属性
警报(myText);
});
};
如何迭代.items
中的所有
我尝试了不同的方法,但没有得到好的结果。首先,我认为您需要修复列表,因为
的第一个节点必须是
()。设置完成后,您可以执行以下操作:
$(function() {
$('.phrase .items').each(function(i, items_list){
var myText = "";
$(items_list).find('li').each(function(j, li){
alert(li.text());
})
alert(myText);
});
};
// note this array has outer scope
var phrases = [];
$('.phrase').each(function(){
// this is inner scope, in reference to the .phrase element
var phrase = '';
$(this).find('li').each(function(){
// cache jquery var
var current = $(this);
// check if our current li has children (sub elements)
// if it does, skip it
// ps, you can work with this by seeing if the first child
// is a UL with blank inside and odd your custom BLANK text
if(current.children().size() > 0) {return true;}
// add current text to our current phrase
phrase += current.text();
});
// now that our current phrase is completely build we add it to our outer array
phrases.push(phrase);
});
// note the comma in the alert shows separate phrases
alert(phrases);
工作
有一件事是,如果您获得上层li
的.text()
,您将获得所有子层文本
保留一个数组将允许提取多个短语
编辑: 如果是空的
UL
而没有LI
,这应该会更好:
// outer scope
var phrases = [];
$('.phrase').each(function(){
// inner scope
var phrase = '';
$(this).find('li').each(function(){
// cache jquery object
var current = $(this);
// check for sub levels
if(current.children().size() > 0) {
// check is sublevel is just empty UL
var emptyULtest = current.children().eq(0);
if(emptyULtest.is('ul') && $.trim(emptyULtest.text())==""){
phrase += ' -BLANK- '; //custom blank text
return true;
} else {
// else it is an actual sublevel with li's
return true;
}
}
// if it gets to here it is actual li
phrase += current.text();
});
phrases.push(phrase);
});
// note the comma to separate multiple phrases
alert(phrases);
给出了一个高投票率和观点的答案。我确实找到了这里和其他链接混合的答案 我有一个场景,如果未选择患者,则禁用所有患者相关菜单。(请参阅链接-) 因此,我没有编写长代码,而是通过CSS找到了一个有趣的解决方案
$(文档).ready(函数(){
PatientId变种;
//var PatientId=1;//删除以启用测试,即选择患者
if(typeof PatientId==“未定义”| | PatientId==“”| | PatientId==0 | | PatientId==null){
console.log(PatientId);
$(“#dvHeaderSubMenu a”)。每个(函数(){
$(this.addClass('disabled');
});
返回;
}
})
。已禁用{
指针事件:无;
不透明度:0.4;
}
-
-
-
-
-
-
这对我有用,但我需要知道ul什么时候没有li,所以,在这种情况下,我应该得到“空白”作为文本。可能我的帖子一点也不清楚。我不明白这一行:if(current.children().size()>0){return true;}谢谢你的回答。从技术上讲,这不是有效的标记。但是你可以使用这行代码。我将用一些澄清的注释更新我的答案——现在已经更新了!。我编辑了我的问题。我永远不会使用附加代码。只有ul使用li或// outer scope
var phrases = [];
$('.phrase').each(function(){
// inner scope
var phrase = '';
$(this).find('li').each(function(){
// cache jquery object
var current = $(this);
// check for sub levels
if(current.children().size() > 0) {
// check is sublevel is just empty UL
var emptyULtest = current.children().eq(0);
if(emptyULtest.is('ul') && $.trim(emptyULtest.text())==""){
phrase += ' -BLANK- '; //custom blank text
return true;
} else {
// else it is an actual sublevel with li's
return true;
}
}
// if it gets to here it is actual li
phrase += current.text();
});
phrases.push(phrase);
});
// note the comma to separate multiple phrases
alert(phrases);
//css
.disabled{
pointer-events:none;
opacity:0.4;
}
// jqvery
$("li a").addClass('disabled');
// remove .disabled when you are done