简体   繁体   English

如何使用JQuery中的“每个”功能搜索有序列表

[英]How can i search ordered list using the “each” function in JQuery

I want to do this loop using jquery but I can't do it. 我想使用jquery进行此循环,但我做不到。

This is my code 这是我的代码

 this.isFound = function (li, text){ for(var i = 0; i<li.length; i++){ if(li[i].innerHTML == text){ this.showError("Sorry, You can't enter a string twice"); return true; break; } } return false; }; 

This is my JQuery loop 这是我的JQuery循环

 $.each(this.list, function(i, item){ if(this.text == item.innerHTML){ return true; } }); 

How can I do that using each or grep or any function else in JQuery?! 我该如何使用JQuery中的each或grep或任何其他函数来做到这一点? thanks in advance 提前致谢

You can use $.grep() to return an array of matches, or .filter() to return a jQuery collection of elements, where the return value of the callback should be Boolean true or false 您可以使用$.grep()返回匹配数组,或使用.filter()返回jQuery元素集合,其中回调的返回值应为Boolean truefalse

$.grep()

 $(function() { var text = "abc"; var res = $.grep($("li"), function(el, index) { return el.textContent === text }); // `res` is an array containing one or more `DOM` elements console.log(res); res[0].style.color = "red"; }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <ul> <li>abc</li> <li>def</li> <li>ghi</li> </ul> 

.filter()

 $(function() { var text = "def"; var res = $("li").filter(function(index, el) { return el.textContent === text }); // `res` is a jQuery object containing one or more `DOM` elements // where jQuery methods can be chained console.log(res); res.css("color", "green"); }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <ul> <li>abc</li> <li>def</li> <li>ghi</li> </ul> 

If you are trying to determine if a given element has .textContent , and if true do stuff, you can use .is() , which returns a Boolean ; 如果您要确定给定元素是否具有.textContent ,并且如果为true ,则可以使用.is() ,它返回一个Boolean ; either true or false truefalse

 $(function() { var text = "ghi"; var i = 0; var res = $("li").is(function(index, el) { i = index; return el.textContent === text }); // `res` is a `Boolean` console.log(res); if (res) { alert(text + " has already been set"); $("li").eq(i).css("color", "gold"); } }) 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <ul> <li>abc</li> <li>def</li> <li>ghi</li> </ul> 

暂无
暂无

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

相关问题 如何使用javascript在有序列表中显示文本 - how can I display the text in an ordered list using javascript 如何使用有序列表将行号添加到文本区域? - How can I add line numbers to a textarea using an ordered list? 我如何动态地将有序列表编号应用于每一行以引导行 - How can i apply ordered list number to each row dynamically to bootstrap rows jQuery:每次我按下一个提交按钮时,就会使用JSON数组中的每个对象更新html有序列表 - jQuery: Update a html ordered list with each object from a JSON array each time I press a submit button .each或搜索功能,如何使用这些功能? - .each or search function, how can I make use of those? jQuery如何将我的有序列表转换成数组? - How can jQuery convert my ordered list into an array? 我如何在jquery中创建一个可单击的列表,其中列表中的每个项目都是其自己的链接,而不仅仅是使用jquery / ajax / json - How can I make a clickable list in jquery where each item in the list is it own link not using HTML just jquery/ajax/json 如何使用二进制搜索方法创建缩小字符串的jQuery函数? - How can I make a jQuery function that shrinks a string using a binary search method? 每当用户使用jQuery在选择下拉列表中选择特定值时,如何执行功能? - How can I perform a function each time that the user select a specific value into a select dropdown using jQuery? 如何使用每个 function ZD223E1439188E4578349D542 在两个相同的 class 名称上添加 class? - How can i add a class on two same class name using a each function jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM