简体   繁体   English

如何从ul li获取单独的值

[英]How to get the separate values from ul li

I want to get the values from the ul li. 我想从ul中获取价值。 I am able to get the values but the problem is that my function gets the values of all the list items in the ul. 我可以获取值,但问题是我的函数获取ul中所有列表项的值。 here is my code 这是我的代码

<div class = "col-sm-8 col-md-8" align = "center">
<div class="input-group">
<div class="input-group-btn search-panel">
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown">
<span id="search_concept">Filter by</span> <span class="caret"></span>
                </button>
                <ul id = "filter" class="dropdown-menu" role="menu">
                  <li><a href="#contains">value one</a></li>
                  <li><a href="#its_equal">value two</a></li>
                  <li><a href="#greather_than">value three</a></li>
                  <li><a href="#less_than">value four</a></li>
                  <li class="divider"></li>
                  <li><a href="#all">value five</a></li>
                </ul>
</div>
</div>
</div>

Here is the JS code 这是JS代码

var li = document.getElementById("filter");
var choice = (li.textContent || li.innerText);

You can get elements of a ul using this code 您可以使用此代码获取ul的元素

var li = document.getElementById("filter");
var a_elements = li.getElementsByTagName("li");
for (var i = 0, len = a_elements.length; i < len; i++ ) {
   alert(a_elements[ i ].innerText);
}

This piece of Javascript will loop trough the li elements and will retrieve the value for you 这段Java代码将遍历li元素,并为您检索值

var list = document.getElementById("filter");

var items = list.getElementsByTagName("li");
for (var i = 0; i < items.length; ++i) {
  // do something with items[i], which is a <li> element
  console.log(items[i].textContent || items[i].innerText);
}

You can used the jQuery .each 您可以使用jQuery .each

The .each() method is designed to make DOM looping constructs concise and less error-prone. .each()方法旨在使DOM循环结构简洁明了,并且不易出错。 When called it iterates over the DOM elements that are part of the jQuery object. 调用时,它将遍历作为jQuery对象一部分的DOM元素。 Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.... More 每次回调运行时,都会从0开始传递当前循环迭代。更重要的是,回调是在当前DOM元素的上下文中触发的,因此关键字this指向该元素

 $(document).ready(function(){ $( "ul#filter li" ).each(function( index ) { console.log( index + ": " + $( this ).text() ); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class = "col-sm-8 col-md-8" align = "center"> <div class="input-group"> <div class="input-group-btn search-panel"> <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown"> <span id="search_concept">Filter by</span> <span class="caret"></span> </button> <ul id = "filter" class="dropdown-menu" role="menu"> <li><a href="#contains">value one</a></li> <li><a href="#its_equal">value two</a></li> <li><a href="#greather_than">value three</a></li> <li><a href="#less_than">value four</a></li> <li class="divider"></li> <li><a href="#all">value five</a></li> </ul> </div> </div> </div> 

By looking at your HTML markup, it seems that you need the clicked li's text. 通过查看您的HTML标记,似乎您需要单击的li的文本。

You can just add click event listener for each li. 您可以只为每个li添加click事件监听器。

var ul = document.getElementById("filter");
var lis = ul.getElementsByTagName("li");

for(var i=0; i<lis.length; i++) {
  lis[i].addEventListener("click", function(e) {
    alert(this.textContent || this.innerText);
  });
}

Hope this helps. 希望这可以帮助。

Use document.querySelectorAll , returns a list of the elements within the document that match the specified group of selectors. 使用document.querySelectorAll返回文档中与指定选择器组匹配的元素列表。 The object returned is a NodeList. 返回的对象是NodeList。

The Array.from() method creates a new Array instance from an array-like or iterable object. Array.from()方法从类似于数组或可迭代对象的对象创建一个新的Array实例。

 var liElems = document.querySelectorAll('ul#filter li'); var liElemsLikeArr = Array.from(liElems); liElemsLikeArr.forEach(function(elem) { alert(elem.textContent); }); 
 <ul id="filter" class="dropdown-menu" role="menu"> <li><a href="#contains">value one</a> </li> <li><a href="#its_equal">value two</a> </li> <li><a href="#greather_than">value three</a> </li> <li><a href="#less_than">value four</a> </li> <li class="divider"></li> <li><a href="#all">value five</a> </li> </ul> 

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

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