jquery - Scroll to specific tag name with specific class -


here html code looks :

<div class="code">     <declaration class="2">         toto     </declaration>     <identifier class="2">         toto     </identifier>     <identifier class="3">         toto     </identifier>     <identifier class="2">         toto     </identifier> </div> 

and here javascript :

function gotodeclaration(){     $(".code identifier").click(function goto() {         var list = document.getelementsbyclassname($(this).attr('class'));         (var = 0; < list.length; i++) {             if (list[i].nodename === 'declaration')                 $('html, body').animate(                     {scrolltop: list[i].offset().top},                      'fast');             return false;         }     }); } 

what if clic on element tag name identifier, scrolls element tag name declaration, same class identifier element.

nothing happens when clic.

the function called after others working functions :

$(document).ready(function(){ gotodeclaration(); highlightidentifiers(); expandcollapse(); }); 

doing list[i] return html element. problem using jquery function that: list[i].offset().

to solve that, use .eq instead:

$('html, body').animate(     {scrolltop: list.eq(i).offset().top},      'fast'); 

there better way code that. since jquery loaded, use it!

function gotodeclaration(){     $(".code identifier").click(function goto() {         var list = $('declaration.'+$(this).attr('class'));         $('html, body').animate(              {scrolltop: list.offset().top},               'fast');     }); } 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -