HTML element with variable

后端 未结 4 1812
鱼传尺愫
鱼传尺愫 2021-01-15 08:08

In jQuery I can create a HTML element with data:

$(\'\').data(\"field\", { i: i, j: j }).appendTo(\"body\");
<         


        
4条回答
  •  梦毁少年i
    2021-01-15 08:39

    With .dataset:

    document.querySelector('button').dataset.field
    

    This isn't supported in all browsers, so you may need to find a polyfill (or use jQuery).

提交回复
热议问题