Table of Contents
There are two ways to dynamically switch font colors. " >There are two ways to dynamically switch font colors.
The first method
The second method
Home Web Front-end JS Tutorial React.js implements dynamic font color switching description

React.js implements dynamic font color switching description

Mar 14, 2018 pm 12:40 PM
javascript react.js color

This article describes how react.js implements dynamic font color switching. Students who are not familiar with react.js implementing dynamic font color switching can take a look at this article with us. Stop talking nonsense. Let’s take a look directly at how react.js implements dynamic font color switching!

There are two ways to dynamically switch font colors.

The first method

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

<!DOCTYPE html>

<html>

  <head>

    <meta charset="UTF-8" />

    <title></title>

    <script src="https://static.runoob.com/assets/react/react-0.14.7/build/react.min.js"></script>

    <script src="https://static.runoob.com/assets/react/react-0.14.7/build/react-dom.min.js"></script>

    <script src="https://static.runoob.com/assets/react/browser.min.js"></script>

  </head>

  <body>

    <p id="example"></p>

    <script type="text/babel">

      var LikeButton = React.createClass({

        getInitialState: function() {

          return {liked: "yellow"};

        },

        handleClick: function(event) {

          this.setState({liked:"blue"});

        },

        render: function() {

        var text = this.state.liked;

        var style = {

            color:text

        }{/*css样式既可以写在组件内也可以写在组件外*/}

          return (

            <p onClick={this.handleClick} style={style}>{/*这里不能直接用如style={color:"yellow"}这种属性赋予方式*/}

              点我,点我,点我我就变身。

            </p>

          );

        }

      });

      React.render(

        <LikeButton />,

        document.getElementById(&#39;example&#39;)

      );

    </script>

  </body>

</html>

Copy after login

This method can only switch styles with one click.

The second method

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

<!DOCTYPE html>

<html>

  <head>

    <meta charset="UTF-8" />

    <title></title>

    <script src="https://static.runoob.com/assets/react/react-0.14.7/build/react.min.js"></script>

    <script src="https://static.runoob.com/assets/react/react-0.14.7/build/react-dom.min.js"></script>

    <script src="https://static.runoob.com/assets/react/browser.min.js"></script>

  </head>

  <body>

    <p id="example"></p>

    <script type="text/babel">

      var LikeButton = React.createClass({

        getInitialState: function() {

          return {liked: false};

        },

        handleClick: function(event) {

          this.setState({liked:!this.state.liked});

        },

        render: function() {

        var text = this.state.liked?"yellow":"blue";

        var style = {

            color:text

        }

          return (

            <p onClick={this.handleClick} style={style}>

              你我。点我切换状态。

            </p>

          );

        }

      });

 

      React.render(

        <LikeButton />,

        document.getElementById(&#39;example&#39;)

      );

    </script>

  </body>

</html>

Copy after login

There is not much difference between the two. The main reason is that the second method uses the ternary operation operator to achieve click Loop switching function.

Related recommendations:

react.js gives the identifier ref and gets the content

Use a very simple example to understand the idea of ​​react.js high-order components

The above is the detailed content of React.js implements dynamic font color switching description. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to change the color of the Windows 10 taskbar How to change the color of the Windows 10 taskbar Jan 01, 2024 pm 09:05 PM

It is very simple to modify the taskbar color in Windows 10, but many users find that they cannot set it. In fact, it is very simple. Just choose your favorite color in the personalization of the computer. If you cannot change the color, pay attention to the detailed settings. How to change the color of the win10 taskbar Step 1: Right-click on the desktop - click Personalize Step 2: Customize the color area Step 3: Choose your favorite color PS: If you cannot change the color, you can click Color -> Select Color -> Customize->Default windows mode, select dark color.

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Vivo's new X100 series memory, color exposure: all series start at 12+256GB Vivo's new X100 series memory, color exposure: all series start at 12+256GB May 06, 2024 pm 03:58 PM

According to news on May 6, vivo officially announced today that the new vivoX100 series will be officially released at 19:00 on May 13. It is understood that this conference is expected to release three models, vivoX100s, vivoX100sPro, and vivoX100Ultra, as well as vivo's self-developed imaging brand BlueImage blueprint imaging technology. Digital blogger "Digital Chat Station" also released the official renderings, memory specifications and color matching of these three models today. Among them, X100s adopts a straight screen design, while X100sPro and X100Ultra have curved screen designs. The blogger revealed that vivoX100s comes in four colors: black, titanium, cyan, and white. The memory specifications

Do you know how to set the color of WPS document table? Do you know how to set the color of WPS document table? Mar 20, 2024 am 08:19 AM

When we see the table colors in other people's WPS documents, the effects are colorful and beautiful; but we only have monotonous black. If you have to fill the table with colors, I believe many students will do it. However, if you want to set the color of the table in WPS document China, many students will definitely find it confusing! Today, let’s learn how to set the color of the WPS document table. I have put together a document that I hope will be helpful to everyone. The steps are as follows: 1. We need to draw a table in the WPS document and right-click the table where the line color is to be modified. 2. Next, use the mouse to click [right mouse button] on the table; in the pop-up menu, we find [Border and Shading]. 3. At this time, the [Border and Shading] option will be opened.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

See all articles