Acest tutorial arata cum se lucreaza cu legaturile parinte (parent) si copii (children) in jQuery.
- parent se refera la elementul parinte in care este inclus direct alt element, denumit copil (child).
De exemplu, in acest cod HTML:
<div id="parent_tag"> <p id="child_tag">Text oarecare...</p> <span>Alt element copil in "parent_tag".</span> </div>- DIV-ul ("parent_tag") este elementul parinte pentru paragraf (<p>) si <span>. Tag-ul <p> si <span> sunt copii al lui "parent_tag" (DIV-ul).
<div id="thediv"> <p id="the_p"> <img src="imgs/javascript.gif" alt="Curs JavaScript" class="clsim" width="200" height="30" /> </p> <button id="btn">Click</button> </div> <script type="text/javascript"><!-- $(document).ready(function() { // la clic pe #btn $('#btn').click(function(){ // preia ID-ul elementului parinte al lui ".clsim" var idpr = $('.clsim').parent().attr('id'); alert('ID-ul elementului parinte al imaginii e:\n'+ idpr); }); }); --></script>
<ul> <li class="clsli"><a href="https://marplo.net/javascript/" title="Curs JavaScript">Curs JavaScript</a></li> <li><a href="https://marplo.net/html/" title="Curs HTML">Curs HTML</a></li> <li class="clsli"><a href="http://www.coursesweb.net/jquery/jquery-course" title="Curs jQuery">Curs jQuery</a></li> </ul> <button id="btn2">Click</button> <script type="text/javascript"><!-- $(document).ready(function() { // la clic pe #btn2 $('#btn2').click(function(){ $('a').parent('.clsli').css('background', '#08ed09'); }); }); --></script>
obiect_jQuery.parent().parent()
<ul> <li><a href="https://marplo.net/javascript/" title="Curs JavaScript" class="clsa">Curs JavaScript</a></li> <li><a href="https://marplo.net/html/" title="Curs HTML">Curs HTML</a></li> <li><a href="http://www.coursesweb.net/jquery/jquery-course" title="Curs jQuery" class="clsa">Curs jQuery</a></li> </ul> <button id="btn3">Click</button> <script type="text/javascript"><!-- $(document).ready(function() { // la clic pe #btn3 $('#btn3').click(function(){ $('li').children('.clsa').css('color', '#fe0001'); }); }); --></script>
$(selector:nth-child(n))Aceasta pseudo-clasa este derivata din CSS. Index-ul (n) incepe cu 1 pentru primul copil.
<div id="iddv"> <img src="imgs/circle.gif" alt="Cerc" width="29" height="30" /> <img src="imgs/rhomb.gif" alt="Romb" width="37" height="30" /> <img src="imgs/triangle.gif" alt="Triunghi" width="36" height="32" /> </div> <button id="btn4">Click</button> <script type="text/javascript"><!-- $(document).ready(function() { var ni = 1; var nrims = $('#iddv img').length; // numar de imagini in #iddv // la clic pe #btn4 $('#btn4').click(function(){ // adauga bordura la al "ni-lea" imagine din #iddv $('#iddv img:nth-child('+ ni+ ')').css('border', '2px dashed blue'); ni++; // mareste "ni" cu 1 // sterge evenimentul "click" de la #btn4 daca "ni" e mai mare decat numar imagini if(ni > nrims) $("#btn4").unbind('click'); }); }); --></script>
<ul class="clsul"> <li>Item 1</li> <li>Item 2, cu UL inclus <ul><li>Item inclus 1</li><li>Item inclus 2</li></ul> </li> <li>Item 3</li> </ul><button id="btn5">Click</button> <script type="text/javascript"><!-- $(document).ready(function() { // la clic pe #btn5 $('#btn5').click(function(){ $('ul.clsul > li').css('border', '1px solid #fe0001'); }); }); --></script>
<ul class="clsul"> <li>Item 1</li> <li>Item 2, cu UL inclus <ul><li>Item inclus 1</li><li>Item inclus 2</li></ul> </li> <li>Item 3</li> </ul><button id="btn6">Click</button> <script type="text/javascript"><!-- $(document).ready(function() { // la clic pe #btn6 $('#btn6').click(function(){ $('ul.clsul li').css('border', '1px solid #fe0001'); }); }); --></script>
<p>Adresa: <strong>http://CoursesWeb.net/</strong> - Tutoriale.</p>
#id { font-weight: 800; }
function someFunction() { alert("CoursesWeb.net"); } setInterval("someFunction()", 2000);
$vname = 8; echo $vname;
We have traveled in space. - Noi am calatorit (deja) in spatiu.
La niña pintaba un arco iris. - Fetita picta un curcubeu.