Monday, 15 August 2011

jquery data attribute as selector: how to get value from attribute inside onclick function? -


Is the best way to get value from any data attribute?

  $ ('[data-scroll]'). ('Click', function (event) {$ (document) .scrollTo ('#' + # data ?, {duration: 'slow'}); return false;});  
  & lt; Script src = "https://ajax.googleapis.com/ajax/libs/JQuery/1.1.1/jquery.min.js" & gt; & Lt; / Script & gt; & Lt; Scroll a Data = "# ScrollTom" href = "#" & gt; Test & lt; / A & gt; & Lt; Br> Br> Br> Br> & Lt; & Lt; & lt & lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Div id = "scrolltome" & gt; & Lt; / Div & gt;  

How can I get value (shortest route)?

already this and data () Use suggested. The catch is that Scroll needs to be specified in lowercase letters.

  $ (this) .data ('scrollto');  

As your snippet ... scrollTo () is not a part of jQuery but perhaps its one of the plugins to do it with jQuery only, ScrollTop () :

  $ ('[data-scrollTo]' ) {$ ('Html, body'). ({ScrollTop: $ ($ (this). Data ('scrollto')) offset (). Top}, 2000);});  < / Pre> 
  & lt; script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.1 / Jquery .min.js "& gt; & Lt; / a & gt; 
;; Br & gt; & lt; & lt; & lt & lt; br & gt; & lt; br & gt; & lt; br & gt; & lt; br & gt; & lt; br & Lt; & lt; & gt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Br> & Lt; Div id = "scrolltome" & gt; Target

No comments:

Post a Comment