How do I use the data- attribute to pass values with jQuery?

前端 未结 4 1482
后悔当初
后悔当初 2021-02-19 07:17

I\'d like to know more about using data-* attributes with HTML and its use within jQuery. I\'m simply trying to retrieve data-attribute-name=\"a value\" with .data() and

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 08:07

    You can find any element attribute using

    $("#someID").attr('attr1') [or] $("#someID").attr('data-attr-1');
    

    to target based on if the element has the attribute, you can do:

    $("[attr1]") or $("[attr1='this is attr 1']") , which will return the element (not the attribute value)

提交回复
热议问题