How to Set js effects for the before and after pseudo elements _ javascript skills

Source: Internet
Author: User
This article introduces how to set js effects for before and after pseudo elements through examples. If you are interested in js pseudo element knowledge, learn about Cascading Style Sheets (CSS) the main purpose is to add styles to HTML elements. However, it is unnecessary or impossible to add additional elements to documents in some cases. In fact, CSS has a feature that allows us to add additional elements without disturbing the document itself. This is a "pseudo element ".

Previous

Cannot directly set js effects for the before and after pseudo elements

Example

Now you need to add (the id is box, the content is "I am test content" p) (: The before content is a "prefix", and the color is a red pseudo class)

  
   Document  

I'm testing content

Script var oBox = document. getElementById ('box'); script

Solution

[Method 1] dynamically embed CSS styles

IE8-the browser changesScript oBox. className = 'change'; script

[Disadvantage] This method cannot control the value of the content attribute in the pseudo element.

Method 3: Use setAttribute to implement custom content

Script oBox. setAttribute ('data-beforedata', 'prefix'); script

[Note] This method can only be implemented using setAttribute. The dataset method is invalid after testing.

Method 4: Add a style sheet

Firefox does not support the addRule () method, and IE8-Explorer does not support the insertRule () method. The Compatibility Statement is as follows:

Script function insertRule (sheet, ruleKey, ruleValue, index) {return sheet. insertRule? Sheet. insertRule (ruleKey + '{' + ruleValue + '}', index): sheet. addRule (ruleKey, ruleValue, index);} insertRule (document. styleSheets [0], '# box: before', 'content: "prefix"; color: red; ', 0) script

[Disadvantage] This method must have an internal

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.