Using data attributes with jQuery

后端 未结 4 1903
我寻月下人不归
我寻月下人不归 2020-12-03 03:19

I have this button:

相关标签:
4条回答
  • 2020-12-03 03:40

    I think data will look on lowercases: alert($(this).data("themevalue")) //grid

    or if you want to use themeValue you need to use:

    edit:

    I was wrong, it doesnt have anything to do with lowercases, you can use themeValue if you are having the attribute: data-theme-value then you call itwith $(element).data("themeValue")

    <button class="themeChanger" data-themeValue="Theme1" data-theme-value="Theme2"></button>
    
    $(".themeChanger").click(function() {
        var el = $(this);
    
        alert($(this).data("themeValue")); //Theme2
        alert($(this).data("themevalue")); //Theme1
    });
    
    0 讨论(0)
  • 2020-12-03 03:55

    I think it is the camel casing of hyphenated words in the data tag implementation that is the gotcha here

    Try this jsfiddle - http://jsfiddle.net/FloydPink/fb6Y6/

    <button type="button" class="themeChanger" data-theme-value="grid" value="Grid">
       data-theme-value                
    </button>
    
    <button type="button" class="themeChanger" data-themeValue="grid" value="Grid">
       data-themeValue
    </button>
    
    $(".themeChanger").click(function () {
        alert($(this).attr("data-themeValue"));
        alert($(this).data("themeValue"));
    });
    
    0 讨论(0)
  • 2020-12-03 03:57

    As noted in this Learning jQuery article, HTML5 data-* attributes are handled by the browser->JS conversion in the same way that CSS names are handled--that is:

    1. the leading data- is removed (a step not needed in the CSS name comparison I drew above)
      • data-specialInfo becomes specialInfo
      • data-more-specialInfo becomes more-specialInfo
    2. the remaining attr name is split on -
      • specialInfo becomes [ specialInfo ]
      • more-specialInfo becomes [ more, specialInfo ]
    3. the first of the resulting split parts is dropped to all lower case
      • [ specialInfo ] becomes [ specialinfo ]
      • [ more, specialInfo ] becomes [ more, specialInfo ] (no change as first part was already lower)
    4. the rest of the resulting split parts are dropped to lower case, but their first letter is made upper case
      • [ specialinfo ] becomes [ specialinfo ] (no change because there were no other parts)
      • [ more, specialInfo ] becomes [ more, Specialinfo ]
    5. The now case-modified parts are rejoined on empty string
      • [ specialinfo ] becomes specialinfo
      • [ more, Specialinfo ] becomes moreSpecialinfo

    This being the case, your data-themeValue attribute is accessible via $(this).data("themevalue"). Whereas a data-theme-value attribute would be accessible via $(this).data("themeValue").

    It's terribly confusing unless you recognize the mechanism in use.

    0 讨论(0)
  • 2020-12-03 04:02

    The problem is the camel case. For clarity I'd stick to the data-theme-value format for your attributes.

    http://jsfiddle.net/NkHEx/2/

    jquery automatically converts .data('some-value') to data('someValue')

    Note that both alert calls return grid

    0 讨论(0)
提交回复
热议问题