Py学习  »  Jquery

如何通过(.each循环)获取jquery中未排序html列表的索引值?

shubham • 5 年前 • 1451 次点击  

我正试图通过jquery中的每个循环获取无序列表的索引值,我正在努力获取这些值。 我只需要jquery每个循环的解,请帮助。

<!DOCTYPE html>
<html>``
<head>
<title></title>

<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>

<script type="text/javascript">

     $(document).ready(function(){
     $('li').each(function(index){
     //do stuff
     console.log(index);
    });
    });

</script>

</head>

<body>

     <ul>
          <li><a href="#">Link 1</a></li>
          <li><a href="#">Link 2</a></li>
          <li><a href="#">Link 3</a></li>
     </ul>

</body>
</html>
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/41044
 
1451 次点击  
文章 [ 2 ]  |  最新文章 5 年前
Eriks Klotins
Reply   •   1 楼
Eriks Klotins    6 年前

this 应该这样做并将新元素附加到页面

$(document).ready(function(){
 $('li a').each(function(index){
 console.log(index, $(this).text());
 $(['<h1>',index," ",$(this).text(),'</h1>'].join('')).appendTo('body');
});
});
muneebShabbir
Reply   •   2 楼
muneebShabbir    6 年前

你可以试试这个,我希望会有帮助

$(document).ready(function(){
   $('li a').each(function(index){
     $(this).text(index + " " + $(this).text());
   });
});