如何在嵌套中获得的偏移量?

时间:2021-01-20 18:55:28

I am having the following HTML

我有以下的HTML。

<ul>
<li>List item one</li>
<li>List item two with subitems:
    <ul>
        <li>Subitem 1</li>
        <li>Subitem 2</li>
    </ul>
</li>
<li>Final list item</li>

I am trying to find offset() of all 'li' including sub list 'li'. But it always returns same value for all 'li'

我试图找到抵消()的所有‘李’‘李’包括子列表。但它总是返回相同的值为所有“李”

script

脚本

$('li').offset().left;

Is this correct? How to get the offset for all 'li' ?

这是正确的吗?如何得到所有“李”的偏移量?

2 个解决方案

#1


2  

You need to loop with each like

你需要与每个像循环

$('li').each(function(){
     alert($(this).offset().left);
});  

#2


1  

use each() to loop through all the li and get the corresponding offset()

使用each()循环遍历所有li并得到相应的偏移量()

 $('li').each(function(){
    console.log($(this).offset().left); 
 });

fiddle

小提琴

#1


2  

You need to loop with each like

你需要与每个像循环

$('li').each(function(){
     alert($(this).offset().left);
});  

#2


1  

use each() to loop through all the li and get the corresponding offset()

使用each()循环遍历所有li并得到相应的偏移量()

 $('li').each(function(){
    console.log($(this).offset().left); 
 });

fiddle

小提琴