Modifying the Pseudo-class style of DOM elements dynamically

Source: Internet
Author: User

Recently write code, you need to modify the pseudo-class content property, do not want to define two styles to switch, but directly through JS to modify.

Pseudo-classes in HTML (such as A:hover/a:link/class::before/clss::after, etc.), because they are not DOM elements, so

It is not possible to modify its properties directly through JS, although it can be modified by toggling the CSS style name of the DOM element, but it is more complicated.

Referring to a post and then made some changes, probably the following ideas:

This works with Windows 8, where JS code is written directly in the Default.js file.

1. In the HTML page, add a span and a button:

<id= "Span_test"></span><  /><type= "button"  value= "Change"  onclick= "changecontent ()"/>

2. In the style sheet, define the display style for this span:

#span_test:: Before {    content: attr (data-content);}

3. In the event that the page UI is loaded, add the Data-content attribute to the element (fortunately HTML5 provides data-):

Args.setpromise (WinJS.UI.processAll (). Then (function  () {     //  can put data-content= ' Smooth ' document.getElementById ("Span_test") on the span label placed directly in the HTML file     . SetAttribute ("Data-content", "HD");  } ));


4. When you click the button, modify the Data-content property of the span element:

function changecontent () {    document.getElementById("Span_test"). SetAttribute ("Data-content", "fluent");   } 


Display effect:

Default:

Click the button:

Demo Download: dom_content (JS modified element Pseudo-Class)

Modifying the Pseudo-class style of DOM elements dynamically

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.