将自动增量值添加到li元素 [英] Adding auto increment value to li element

查看:78
本文介绍了将自动增量值添加到li元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名css /设计师,所以请原谅我的跛脚,不知道任何.js

im a css/designer guy so please excuse my lameness in not knowing any .js

基本上我想知道如何将自动增量ID添加到列表中项目与javascript / jquery为我试图添加一些CSS的东西。

basically i want to know how to add an auto incremental id to a list item with javascript / jquery for something that i am trying to add some css to.

之前

<li id=""><a href="">Item number 1</a></li>
<li id=""><a href="">Item number 2</a></li>
<li id=""><a href="">Item number 3</a></li>

<li id="1"><a href="">Item number 1</a></li>
<li id="2"><a href="">Item number 2</a></li>
<li id="3"><a href="">Item number 3</a></li>

提前感谢,尤其是阅读本文

thanks in advance and especially just for reading this

尝试了所有的回复,除了ul / li项目之外什么都没有在简单的html页面上工作。

tried all the responses, nothing has worked on a plain html page with nothing but the ul/li items.

感谢所有尝试,我失败了一个很大的方式.....我不是编码器

thanks to all that tried, i have failed in a big way.....im not a coder

推荐答案

我要给你的 li 使用 id 标记包含 ul ,以防其他 li 页面上您不想订购的标签,但在jQuery中这很容易:

I'm going to give your li tags an encompassing ul with an id in case there are other li tags on the page that you don't want to order, but in jQuery this is pretty easy for:

<ul id="ordered">
    <li><a href="">Item number 1</a></li>
    <li><a href="">Item number 2</a></li>
    <li><a href="">Item number 3</a></li>
</ul>

您只需使用每个方法:

$('#ordered li').each(function(i,el){
    el.id = i+1;
});

我建议使用除了普通整数以外的其他东西,所以也许像'ordered'+(i + 1)而不只是 i + 1 以上。

I would recommend using something other than just a plain integer for an id though, so maybe something like 'ordered' + (i+1) instead of just i+1 above.

这篇关于将自动增量值添加到li元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆