你为什么要给一个样式标签一个id

Dav*_*vid 28 html css

您已经注意到一些网站为样式标记提供了ID,例如:

<style id=style-id></style>
Run Code Online (Sandbox Code Playgroud)

任何人都可以先解释为什么你会这样做以及doin的好处吗?

Dan*_*eam 51

所以你可以引用它(就像任何其他元素一样),即

var styles = document.getElementById('style-id');
// do anything you want, like
styles.parentNode.removeChild(styles); // remove these styles
styles.setAttribute('href', 'alternate-styles.css'); // change the style
Run Code Online (Sandbox Code Playgroud)

  • 因此您可以使用它来使用javascript创建动态样式吗? (3认同)