简体   繁体   English

如何获得锚的第二个元素上的CSS左属性值

[英]How to get CSS left property value on second element of anchor

I have this HTML Structure: 我有这个HTML结构:

<div id="sqm-id" class="ui-state-disabled ui-slider ui-slider-horizontal ui-widget ui-widget-content ui-corner-all ui-slider-disabled ui-disabled">
    <div class="ui-slider-range ui-widget-header" style="left: 0%; width: 100%;"></div>
    <a href="#" class="ui-slider-handle ui-state-default ui-corner-all" style="left: 0%;"></a>
    <a href="#" class="ui-slider-handle ui-state-default ui-corner-all" style="left: 100%;"></a>
</div>

And I am trying to find second anchor inline left value and I have tried with this jQuery code but everytime I am getting undefined from console. 我试图找到second anchor内联left值,并且尝试使用此jQuery代码,但是每次从控制台获取undefined值时,我都会尝试。

console.log($("#sqm-id").find("a:eq(2)").css("left"));

JSFiddle: Sample Demo JSFiddle: 示例演示

Any ideas? 有任何想法吗?

eq() accesses elements by their index, which is zero-based. eq()通过其索引(从零开始eq()访问元素。 Therefore to get the second element you need :eq(1) . 因此,要获取第二个元素,您需要:eq(1)

console.log($("#sqm-id").find("a:eq(1)").css("left"));

Updated fiddle 更新的小提琴

Another way use nth-of-type() the index of each child to match, starting with 1 compared to .eq() index it starts from zero array based selector 另一种使用nth-of-type()来匹配每个子代的索引的方法,从1开始,而.eq()索引则从基于零数组的选择器开始

console.log($("#sqm-id").find("a:nth-of-type(2)").css("left"));

Fiddle 小提琴

you can use eq() start with zero index and our anchor tag at second position on div so we us eq(1) 您可以使用从零索引开始的eq()和位于div上第二个位置的锚标记来使用eq(1)

And if we use #sqm-id a it means all anchor tag inside that id #sqm no need to use .find() 如果我们使用#sqm-id ,则表示该ID #sqm所有锚标签都无需使用.find()

console.log($('#sqm-id a').eq(1).css("left"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM