关于jquery基本过滤器中:eq()无法传变量的问题

jquery基本过滤器:eq()是没有办法传变量的,只能传递常数

例如有html

1 <body>
2     <ul>
3         <li>aaa</li>
4         <li>bbb</li>
5         <li>ccc</li>
6     </ul>
7 </body>

想要根据参数来获取ul中li的内容,不能写成

1     <script>
2         $(function () {
3             req(1);
4         });
5         function req(index) {
6             alert($('ul li:eq(index)').html());
7         }
8     </script>

会发现值为undefined ,可以用字符串拼接的方法使之间接成为常量

1     <script>
2         $(function () {
3             req(1);
4         });
5         function req(index) {
6             alert($('ul li:eq('+index+')').html());
7         }
8     </script>

 

posted @ 2016-04-21 15:15  JDDDD  阅读(860)  评论(0编辑  收藏  举报