2009-09-24 15 views
50

Używanie jQuery w jaki sposób wybrać pojedynczy element podrzędny? Szukałem w API postępowego i wiem, że mogę zaznaczyć wszystkie bezpośrednim dzieciom img elementy tak:Jak wybrać pojedynczy element podrzędny za pomocą jQuery?

$(this).children('img'); 

I, aby wybrać pierwszą dziecko img elementu mogę użyć indeks tak:

$(this).children('img')[0]; 

Ale myślę, że jestem trochę zaskoczony, że nie może tego zrobić:

$(this).child('img'); // no subscript, returns single element 

A może coś mi umknęło?

Odpowiedz

32

Nie. Każda funkcja jQuery zwraca obiekt jQuery i tak to działa. Jest to kluczowa część magii jQuery.

Jeśli chcesz uzyskać dostęp do elementu bazowego, masz trzy opcje ...

  1. Nie używaj jQuery
  2. Używaj [0] odwołać to
  3. Extend jQuery robić, co chcesz. ..

    $.fn.child = function(s) { 
        return $(this).children(s)[0]; 
    } 
    
+0

Ah ha! To było to. Spodziewałem się elementu img (próbując odwołać się do jego właściwości src) zamiast obiektu jQuery. Teraz po prostu użyję 'attr()', aby uzyskać właściwość 'src'. –

+0

Dzięki za 3). Mogę to rozważyć. :) –

+6

Nie. Jeśli użyję na przykład 'find (" div ") [0]' nie otrzymam 'div' jako obiektu jQuery. Otrzymam 'div' jako HTMLElement. Aby uzyskać 'div' jako obiekt jQuery należy użyć' .eq (0) ', jak powiedział Greg poniżej. – Green

80

myślę, co chcesz zrobić to w ten sposób:

$(this).children('img').eq(0); 

to daje obiekt jquery zawierający pierwszy element img, natomiast

$(this).children('img')[0]; 

daje samego elementu img.

+1

Pamiętaj, że użycie find as w pierwszej odpowiedzi da ci wszystkie elementy potomne, podczas gdy dziecko da ci tylko bezpośrednie potomstwo. – Greg

3

Może w ten sposób?

$('img', this)[0] 
+0

To niekoniecznie wybierze bezpośrednie dziecko i, tak jak zaakceptowana odpowiedź, zwróci element, a nie obiekt jQuery. – Septagram

0
<html> 
<title> 

    </title> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.css"> 
<body> 




<!-- <asp:LinkButton ID="MoreInfoButton" runat="server" Text="<%#MoreInfo%>" > --> 
<!-- </asp:LinkButton> --> 
<!-- </asp:LinkButton> --> 

<br /> 
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1"> 
<div> 
<a><span id="imgDownArrow_0" class="glyphicon glyphicon-chevron-right " runat="server"> MoreInformation</span></a> 
<div id="parent" class="dataContentSectionMessages" style="display:none"> 
    <!-- repeater1 starts --> 
     <!-- <sc:text field="Event Description" runat="server" item="<%#Container.DataItem %>" /> --> 
      <ul > 
       <li ><h6><strong>lorem</strong></h6></li> 
       <li ><h6><strong>An assigned contact who knows you and your lorem analysis system</strong></h6></li> 
       <li ><h6><strong>Internet accessible on-demand information and an easy to use internet shop</strong></h6></li> 
       <li ><h6><strong>Extensive and flexible repair capabilities at any location</strong></h6></li> 
       <li ><h6><strong>Full Service Contracts</strong></h6></li> 
       <li ><h6><strong>Maintenance Contracts</strong></h6></li> 
      </ul> 
    <!-- repeater1 ends --> 
</div> 
</div> 
<div> 
<a><span id="imgDownArrow_0" class="glyphicon glyphicon-chevron-right " runat="server"> MoreInformation</span></a> 
<div id="parent" class="dataContentSectionMessages" style="display:none"> 
    <!-- repeater1 starts --> 
     <!-- <sc:text field="Event Description" runat="server" item="<%#Container.DataItem %>" /> --> 
      <ul > 
       <li ><h6><strong>lorem</strong></h6></li> 
       <li ><h6><strong>An assigned contact who knows you and your lorem analysis system</strong></h6></li> 
       <li ><h6><strong>Internet accessible on-demand information and an easy to use internet shop</strong></h6></li> 
       <li ><h6><strong>Extensive and flexible repair capabilities at any location</strong></h6></li> 
       <li ><h6><strong>Full Service Contracts</strong></h6></li> 
       <li ><h6><strong>Maintenance Contracts</strong></h6></li> 
      </ul> 
    <!-- repeater1 ends --> 
</div> 
</div> 
<div> 
<a><span id="imgDownArrow_0" class="glyphicon glyphicon-chevron-right " runat="server"> MoreInformation</span></a> 
<div id="parent" class="dataContentSectionMessages" style="display:none"> 
    <!-- repeater1 starts --> 
     <!-- <sc:text field="Event Description" runat="server" item="<%#Container.DataItem %>" /> --> 
      <ul > 
       <li ><h6><strong>lorem</strong></h6></li> 
       <li ><h6><strong>An assigned contact who knows you and your lorem analysis system</strong></h6></li> 
       <li ><h6><strong>Internet accessible on-demand information and an easy to use internet shop</strong></h6></li> 
       <li ><h6><strong>Extensive and flexible repair capabilities at any location</strong></h6></li> 
       <li ><h6><strong>Full Service Contracts</strong></h6></li> 
       <li ><h6><strong>Maintenance Contracts</strong></h6></li> 
      </ul> 
    <!-- repeater1 ends --> 
</div> 
</div> 
<div> 
<a><span id="imgDownArrow_0" class="glyphicon glyphicon-chevron-right " runat="server"> MoreInformation</span></a> 
<div id="parent" class="dataContentSectionMessages" style="display:none"> 
    <!-- repeater1 starts --> 
     <!-- <sc:text field="Event Description" runat="server" item="<%#Container.DataItem %>" /> --> 
      <ul > 
       <li ><h6><strong>lorem</strong></h6></li> 
       <li ><h6><strong>An assigned contact who knows you and your lorem analysis system</strong></h6></li> 
       <li ><h6><strong>Internet accessible on-demand information and an easy to use internet shop</strong></h6></li> 
       <li ><h6><strong>Extensive and flexible repair capabilities at any location</strong></h6></li> 
       <li ><h6><strong>Full Service Contracts</strong></h6></li> 
       <li ><h6><strong>Maintenance Contracts</strong></h6></li> 
      </ul> 
    <!-- repeater1 ends --> 
</div> 
</div> 




</asp:Repeater> 


</body> 
<!-- Predefined JavaScript --> 
<script src="jquery.js"></script> 
<script src="bootstrap.js"></script> 

<script> 

$(function() { 
     $('a').click(function() { 
      $(this).parent().children('.dataContentSectionMessages').slideToggle(); 
     }); 
    }); 

    </script> 


</html> 
+0

$ (function() { $ ("a"). Click (function() { $ (this) .parent(). Children (". DataContentSectionMessages") slideToggle(); }); }) ; to zapytanie pomoże uzyskać element potomny rodzica –

Powiązane problemy