简体   繁体   中英

Is it a good idea to use html5 data attributes for javascript selectors?

I'm trying to make it really obvious in the DOM that javascript code depends on the structure. Is the following a good idea?

HTML:
<a class='submenu' data-submenu="true" .... </a>

JS:
var mySubnav = $("[data-submenu]");

Makes it really clear that javascript is operating on the html. But I haven't seen this used by other people.

I'm not sure why you can't just use classes for this case? If your links have a subnav class that's far more compatible and easy to parse, plus stylable, than a data-submenu attribute.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM