使用jQuery

时间:2016-11-09 03:08:55

标签: jquery nested

点击项目本身后,我正试图获取嵌套li项的索引值。

正如你在控制台上看到的那样,我可以得到没有正确嵌套的项目的索引值,但是嵌套的项目自然会给我0或1而不是2或3。

有没有办法可以找到li的索引值,即使它位于ul内的另一个div内?

 $('li a').on('click', function() {
   console.log($($(this)["0"].offsetParent).find('li') );
   console.log($(this).parent().index() )
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li><a href="#">Item 0</a></li>
  <li><a href="#">Item 1</a></li>
  <div class="group">
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a></li>
  </div>
  <li><a href="#">Item 4</a></li>
</ul>

1 个答案:

答案 0 :(得分:1)

您正在以错误的方式使用let view = UIView(frame:CGRect(x: 0, y: 0, width: 200, height: 150)) let layer = CAShapeLayer() layer.frame = view.layer.bounds // * let triangle = UIBezierPath() triangle.move(to: CGPoint(x: 50, y: 150)) triangle.addLine(to: CGPoint(x: 100, y: 50)) triangle.addLine(to: CGPoint(x: 150, y: 150)) triangle.close() layer.path = triangle.cgPath layer.strokeColor = UIColor.blue.cgColor layer.lineWidth = 3 view.layer.addSublayer(layer) view layer.anchorPoint = CGPoint(x: 0.5, y: 0) // * layer.setAffineTransform(CGAffineTransform(scaleX: 1, y: 0.5)) view 属性,请尝试此代码。

&#13;
&#13;
Dim x = {1, 1, 1, 0, 0, 0, 1, 1}
Dim y = {1, 2, 3, 4, 5, 6, 7, 8}

' using a for loop
Dim sum1 As Integer = 0
For i = 0 To x.Length - 1
    sum1 += If(x(i) = 1, 1, 0) * y(i)
Next
Console.WriteLine(sum1)

' using LINQ #1
Dim sum2 As Integer = x.Select(Function(i, index) If(i = 1, 1, 0) * y(index)).Sum()
Console.WriteLine(sum2)

' using LINQ #2
Dim sum3 As Integer = x.Zip(y, Function(x1, y1) If(x1 = 1, 1, 0) * y1).Sum()
Console.WriteLine(sum3)

' using LINQ #3
Dim sum4 As Integer = Enumerable.Range(0, x.Length).Sum(Function(i) If(x(i) = 1, 1, 0) * y(i))
Console.WriteLine(sum4)

Console.ReadLine()
&#13;
index()
&#13;
&#13;
&#13;