html “data-” attribute as javascript parameter

前端 未结 6 926
说谎
说谎 2020-11-29 00:27

Lets say I have this:

6条回答
  •  执念已碎
    2020-11-29 01:26

    The short answer is that the syntax is this.dataset.whatever.

    Your code should look like this:

    Another important note: Javascript will always strip out hyphens and make the data attributes camelCase, regardless of whatever capitalization you use. data-camelCase will become this.dataset.camelcase and data-Camel-case will become this.dataset.camelCase.

    jQuery (after v1.5 and later) always uses lowercase, regardless of your capitalization.

    So when referencing your data attributes using this method, remember the camelCase:

    Also, you don't need to use commas to separate attributes.

提交回复
热议问题