mat*_*des 6 javascript jquery dom list
如果我ul有3个项目并且list-style-type设置为lower-alpha,我最终得到了这个
一个.第1项
湾 第2项
C.第3项
使用jQuery,我可以轻松获得您点击的任何项目的价值 - 如果我点击第一个项目,则为"项目1".但我可以获得列表项标签吗?在这种情况下一个?
不确定DOM API是否公开了,但你可以......
$('ul').on('click', 'li', function() {
var label = String.fromCharCode(97 + $(this).index());
});
Run Code Online (Sandbox Code Playgroud)
......如果你有26个元素.如果您有更多,则需要使用更复杂的算法,通常称为Excel行到列算法.
$('ul').on('click', 'li', function() {
var index = $(this).index() + 1;
var label = '';
var mod;
while (index) {
mod = (index - 1) % 26;
label = String.fromCharCode(97 + mod) + label;
index = Math.floor((index - mod) / 26);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
956 次 |
| 最近记录: |