将重点放在具有<a> tag in it

时间:2017-07-14 19:28:05

标签: javascript jquery html

Having a bit issue figuring out how to select only the first child in a li tag that has a link in it.

HTML:

<ul class="dropdown-menu" role="menu" aria-expanded="true">
  <li role="presentation" class="dropdown-header">Label</li>
  <li><a href="#">Select Me</a> </li>
</ul>

JavaScript:

dropdown.find('.dropdown-menu li:first-child a').focus();

In this particular case it doesn't get past the list item that is the "Label"

2 个答案:

答案 0 :(得分:1)

您可以使用$('body').find('.dropdown-menu li a').eq(0).css('color','red');方法。

  

eq方法将匹配元素的集合减少到   指定的索引。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="dropdown-menu" role="menu" aria-expanded="true">
  <li role="presentation" class="dropdown-header">Label 1</li>
  <li role="presentation" class="dropdown-header">Label 2</li>
  <li role="presentation" class="dropdown-header">Label 3</li>
  <li><a href="#">Select Me</a> </li>
</ul>
:first

另一种方法是使用$('body').find('.dropdown-menu li a:first').css('color','red'); 伪选择器

  

第一种方法将匹配元素集合减少到第一个   集。

$('body').find('.dropdown-menu li a:first').css('color','red');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="dropdown-menu" role="menu" aria-expanded="true">
  <li role="presentation" class="dropdown-header">Label</li>
  <li><a href="#">Select Me</a> </li>
</ul>
$.each

此外,另一个可以帮助您的解决方案是使用li方法搜索具有超链接元素的第一个 $('ul li').each(function(){ if($(this).find('a').length>0){ $(this).find('a').css('color','red'); return false; } });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul class="dropdown-menu" role="menu" aria-expanded="true">
  <li role="presentation" class="dropdown-header">Label 1</li>
  <li role="presentation" class="dropdown-header">Label 2</li>
  <li role="presentation" class="dropdown-header">Label 3</li>
  <li><a href="#">Select Me</a> </li>
</ul>
Sub test()

    Dim ws As Worksheet
    Set ws = ActiveSheet            ' or point to any worksheet here, if you want  

    Dim CellArray As Range

    Dim myCell As Range             ' range("a:a") must contain "Total checks", otherwise next line will fail
    Set myCell = ws.Cells(Application.WorksheetFunction.Match("Total checks", ws.Range("A:A"), 0), Application.WorksheetFunction.Match(ComboBox1.Value, 0))

    myCell.Select                   ' debug ... visually verify the cell location

    myCell.Formula = "=SUM('" & Root & sourceSheet & ws.Name & " " & monthNumber & "." & lastDay & "." & Format(Now(), "yy") & "'!$H:$H)"

    If CellArray Is Nothing Then
        Set CellArray = myCell      ' CellArray cannot be empty to start, otherwise the "union" function will fail
    Else
        Set CellArray = Union(CellArray, myCell)
    End If

    CellArray.Select                ' debug ... visually verify the resulting CellArray range

End Sub

答案 1 :(得分:1)

$('.dropdown-menu > li > a').first()怎么样?

相关问题