开发者

Attach data to HTML elements in server page generation for use in Javascript

开发者 https://www.devze.com 2023-03-03 21:17 出处:网络
I\'d like to attach id numbers to my list items such that these numbers can be read in JS. It makes most sense to attach the id when generating the HTML on the server. Is there a good way to do this o

I'd like to attach id numbers to my list items such that these numbers can be read in JS. It makes most sense to attach the id when generating the HTML on the server. Is there a good way to do this other than ta开发者_Go百科cking the id to the li's as expando attributes? Or is that the only way other than generating the elements in JS?

To be clear, the expando option would be:

<li myid="{{=item.id}}">{{=item.name}}</li>

This seems icky to me, but I also don't want to generate a big JS list of the item data which is then used to generate the li's. Is there just no 'proper' option?


There are custom data-* attributes for that:

<li data-id="{{=item.id}}">{{=item.name}}</li>

see: http://www.w3.org/TR/html5/elements.html#embedding-custom-non-visible-data-with-the-data-attributes

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号