mardi 28 juin 2016

Hierarchy Element Selection by Class for Window and Attr url


Hello I'm a beginner in related tags, I used as reference: Bind Keyboard to left/right navigation I have this HTML: <div class="pager"> <br /> <div class="myjsp-prev-next" style="text-align:inherit;"> <span class="myjsp-prev"><a href="{url}">Prev</a></span> <span class="myjsp-next"><a href="{url}">Next</a></span> </div> </div> <br /> Javascript: $(function() { $(document).keyup(function(e) { switch (e.keyCode) { case 37: var x = document.querySelectorAll(".myjsp-prev"); window.location = x.attr('href'); break; case 39: window.location = $('span.myjsp-next a').attr('href'); break; } }); }); I was reading the API for JQUERY and wanted to try to use the querySelector to select the elements with that class and then go to the next or prev url. I was reading the hierarchy page for JQUERY and I'm not sure what is wrong. Thanks in advance. EDIT: I tried adding: <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(function() { $(document).keyup(function(e) { switch (e.keyCode) { case 37: var x = document.querySelectorAll(".myjsp-prev a")[0]; var href = x.getAttribute('href'); window.location = x.attr('href'); break; case 39: var y = document.querySelectorAll(".myjsp-next a")[0]; var href = y.getAttribute('href'); window.location = y.attr('href'); break; } }); }); </script> I get the error in console log: Uncaught TypeError: y.attr is not a function Uncaught TypeError: x.attr is not a function I also tried eq(x) and eq(y), but still doesn't work. Thank you.

Aucun commentaire:

Enregistrer un commentaire