简体   繁体   English

如何从ul列表中的第一个li获取ID

[英]How to get the id from the first li in a ul list

I am trying to use Javascript and Jquery to get the id value of a ul list from an array I generate from the list the arrays value is lis but I cant get the id value out of that I have tried to use .innerHTML but that outputs the whole thing and not just the id? 我正在尝试使用Javascript和Jquery从我从列表生成的数组中获取ul列表的id值,该数组的值为lis,但是我无法获得尝试使用.innerHTML的id值,但输出整个事情,而不仅仅是ID? .value outputs nothing and .id also outputs nothing. .value不输出任何内容,.id也不输出任何内容。 What should I do to get the id from the array lis. 我应该怎么做才能从数组lis获取ID。 the code is below 代码在下面

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Sortable - Default functionality</title>

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

  <style>
  #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
  #sortable li span { position: absolute; margin-left: -1.3em; }
  </style>
  <script>
  $(function() {
    $( "#sortable" ).sortable();
   $( "#sortable" ).disableSelection();
});

  </script>
</head>
<body>

<ul id="sortable" class="dummy" >
  <li class="ui-state-default" value="aa" ><span class="ui-icon ui-icon-arrowthick-2-n-s" id="1a" ></span>Item 1</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s" id="2b" ></span>Item 2</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s" id="3c" ></span>Item 3</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s" id="4d" ></span>Item 4</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s" id="5e" ></span>Item 5</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s" id="6f" ></span>Item 6</li>
  <li class="ui-state-default"><span class="ui-icon ui-icon-arrowthick-2-n-s" id="7g" ></span>Item 7</li>
</ul>
 <script>
var theObject = document.getElementById("sortable");
var lis = theObject.children;
alert(lis[0].innerHTML.value);
</script>

</body>
</html>

Given this list 给定这个清单

<ul>
    <li id="1"></li>
    <li id="2"></li>
    <li id="3"></li>
</ul>    

You could use this code to get the ids of all <li> : 您可以使用以下代码获取所有<li>的ID:

map=Function.prototype.call.bind([].map)
list=document.querySelectorAll("li");
ids=(map(list, function(x){return x.id;}))

Here is the Fiddle to play with. 这是小提琴演奏。 Look at MDN Documentation for Array.map() . 查看MDN文档中的Array.map()

var ulElement = document.getElementById('sortable');
var liElements = ulElement.getElementsByTagName('li');

var spanElements = liElements[0].getElementsByTagName('span');
var idOfInterest = spanElements[0].id;

This code will set the idOfInterest to '1a' 此代码会将idOfInterest设置为“ 1a”

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

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