How do I read and write pseudo-class Element styles ?, Read/write pseudo element styles

Source: Internet
Author: User

How do I read and write pseudo-class Element styles ?, Read/write pseudo element styles

Example:

Html code
  1. <P class = "example" data-pseudo = "(I'm content.)"> Hi, this is a plain-old, sad-looking paragraph tag. </p>
  2. <Div id = "log"> </div>
  3. <Style>
  4. . Example: before {
  5. Content: 'false ';
  6. Color: red;
  7. }
  8. </Style>



1. Read data using the window. getComputedStyle method: 

Js Code
  1. <Script>
  2. Var str = window. getComputedStyle (document. querySelector ('. example'), ': before'). getPropertyValue ('color ');
  3. Document. querySelector ('# log'). innerHTML = str;
  4. </Script>



Ii. Write (modify)
 

No way. At least there is no direct method, and we can only achieve our goal by overwriting the style.
For example, add a style element and write a new style, or add different classes to the target element to achieve the control effect.

Example:

Html code
  1. <Style id = "pseudo dostyle">
  2. </Style>



Js Code
  1. <Script>
  2. Document. querySelector ('# pseudo style'). sheet. insertRule (". example: before {color: green;}", 0 );
  3. // Or
  4. Document. querySelector ('# pseudostyle'). sheet. addRule ('. example: before ', 'color: green ');
  5. // SetProperty will throw an exception at this time:
  6. Try {
  7. Window. getComputedStyle (document. querySelector ('. example'), ': before'). setProperty ('color', "green ");
  8. } Catch (e ){
  9. Document. querySelector ('# log'). innerHTML = e;
  10. }
  11. </Script>



A pseudo-class element has a special attribute: content. Because its value can be defined to be obtained from the dom attribute of the element, it can implement direct reading and writing of js.
For example:

Html code
  1. <Style>
  2. . Example: before {
  3. Content: attr (data-pseudo );
  4. Color: red;
  5. }
  6. </Style>



Js Code
  1. <Script>
  2. Document. querySelector ('. example'). setAttribute ("data-pseudo", "new content! ");
  3. </Script>



Appendix:
1. styleSheet object method, attributes and browser compatibility, refer to: http://help.dottoro.com/ljpatulu.php
2. getComputedStyle method, reference: http://help.dottoro.com/ljscsoax.php, https://developer.mozilla.org/en/docs/Web/API/window.getComputedStyle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.