简体   繁体   English

更改单独文件中文本区域的颜色

[英]Changing the color of a textarea in a separate file

The issue is I'm trying to change the color of a textarea when a radio button is clicked.问题是我试图在单击单选按钮时更改 textarea 的颜色。 There's 4 radio buttons so the textarea needs to be able to change color 4 times for 4 different buttons.有 4 个单选按钮,因此 textarea 需要能够为 4 个不同的按钮更改颜色 4 次。 I'm in reactjs.我在 reactjs 中。 I've looked up this problem and wasn't able to get any decent pointers.我查了这个问题,但没有得到任何像样的指针。 I've tried calling the textbox function from control.js but it doesn't do anything.我试过从 control.js 调用 textbox 函数,但它没有做任何事情。 I tried to use props but it didn't really work and I'm not 100% sure on how to use it.我尝试使用道具,但它并没有真正起作用,而且我不是 100% 确定如何使用它。 What this looks like:这看起来像什么:

controls.js (has the radio buttons) control.js(有单选按钮)

import React from 'react';
import './App.css';
import TextBox from './textbox';

class Controls extends React.Component{

    constructor(props){
        super(props);
        this.state = {
            tcolor: "black"
        };
    }    
    
    changeColor(id){
        switch(id){
            case "Blue":
                //TextBox.getElementById("origin").style.color = "blue";
                //this.setState({tcolor: "blue"});
                TextBox.blue();
                //document.getElementById("origin").style.color = "blue";
                //TextBox.document.getElementById("extra").style.color = "blue";
                break;
            case "Red":
                TextBox.red();
                //this.setState({ tcolor: "red" });
                break;
            case "Green":
//more code

render(){
        return (
            <div>
                <label for="colours">Colors</label><br />
                <input type="radio" id="Blue" onclick={() => this.changeColor("Blue")} name="colours" />
                <label for="Blue">Blue</label><br />
                <input type="radio" id="Red" onclick={() => this.changeColor("Red")}name="colours" />
//more code

Textbox.js:文本框.js:

class TextBox extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            hideBox: false,
            butt: "more",
        };
    }
    
    blue() {
        document.getElementById("origin").style.color = "blue";
        document.getElementById("extra").style.color = "blue";
    }
//the rest of the color functions

render(){
        return(
            <div>
                <div>
                    <textarea id="origin" name="origin" rows="8" cols="48">Stuff inside
                    </textarea>
                </div>);
}

Both files are properly exported.这两个文件都已正确导出。

You need to have a main component, let's say App.js.您需要有一个主要组件,比如说 App.js。 In it, you:在其中,您:

  • hold the state (the active background colour)保持状态(活动背景颜色)
  • render the Controls component (passing in a prop that is a function called “setBackgroundColour”)渲染控件组件(传入一个名为“setBackgroundColour”的函数的道具)
  • render the Textbox component (passing in a prop that is a string called “backgroundColour)渲染 Textbox 组件(传入一个名为“backgroundColour”的字符串的 prop)

You can definitely remove the TextBox altogether as you mentioned it was a try.正如您提到的那样,您绝对可以完全删除 TextBox。

Your initial approach looks correct;你最初的方法看起来是正确的; to set the state tcolor using:使用以下方法设置状态tcolor

changeColor(id){
  switch(id){
    case "Blue":
      this.setState({tcolor: "blue"});
      break;
    ...
}

You need to use this tcolor as in your render function wherever needed.您需要在需要时在渲染函数中使用此 tcolor。 Eg:例如:

render() {
  const style = {
      color: this.state.tcolor
  };
  return(
    <div>
      <textarea style={style} id="origin" name="origin" rows="8" cols="48">Stuff inside</textarea>
    </div>
  );
}

React will take care updating it everytime the state changes.每次状态改变时,React 都会小心地更新它。 Note: It's almost always a bad idea to use DOM functions like getElementById() in react.注意:在 react 中使用像getElementById()这样的 DOM 函数几乎总是一个坏主意。

You should render in the right way your TextBox component, at first try this:你应该以正确的方式呈现你的 TextBox 组件,首先试试这个:

render(){
        return (
            <div>
                <TextBox color={this.state.tcolor}/>
                <label for="colours">Colors</label><br />
                <input type="radio" id="Blue" onclick={() => this.changeColor("Blue")} name="colours" />
                <label for="Blue">Blue</label><br />
                <input type="radio" id="Red" onclick={() => this.changeColor("Red")}name="colours" />
//more code

The second point for you is a change state in changeColor function:您的第二点是 changeColor 函数中的更改状态:

     changeColor(color){
       this.setState({tcolor: color});
     }

After function call, your state should update and your TextBox component get new props, then you can get it in your component:函数调用后,您的状态应该更新并且您的 TextBox 组件获得新的道具,然后您可以在您的组件中获取它:

class TextBox extends React.Component{
    constructor(props){
        super(props);
        this.state = {
            hideBox: false,
            butt: "more",
        };
    }

   const style = {
     backgroundColor: this.props.color;
   }

render(){
        return(
            <div>
                <div>
                    <textarea style={style} id="origin" name="origin" rows="8" cols="48">Stuff inside
                    </textarea>
                </div>);
}

I also want to add a few recommendations for you:我还想为您添加一些建议:

  1. Don't write document.getElementById("extra").style.color = "blue";不要写document.getElementById("extra").style.color = "blue"; , it's bad practice for React, because you contacting directly to DOM, not in React, in React you should manage date in your app throw state, but don't try change data directly in DOM; ,这对 React 来说是不好的做法,因为你直接接触 DOM,而不是在 React 中,在 React 中你应该在你的应用程序抛出状态中管理日期,但不要尝试直接在 DOM 中更改数据;
  2. Define state only in a component which you want to manage your data or manage something in child components;仅在要管理数据或管理子组件中的某些内容的组件中定义状态;
  3. Try using the functional components in React, it's pretty simple;尝试使用 React 中的函数式组件,非常简单;
  4. Define color names correctly, with a lowercase letters;正确定义颜色名称,使用小写字母;
  5. Read react documentation to better figure out in library: https://reactjs.org/阅读反应文档以更好地在库中找出答案: https : //reactjs.org/

try using background-color instead of color.尝试使用背景颜色而不是颜色。 So instead of calling document.getElementById('YOUR ID').style.color , which would just change the text color, use document.getElementById('YOUR ID').background.color = 'color.'因此,不要调用document.getElementById('YOUR ID').style.color ,这只会改变文本颜色,而是使用document.getElementById('YOUR ID').background.color = 'color.' Besides this, just changing the color with each different button should work fine...除此之外,只需更改每个不同按钮的颜色就可以正常工作......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM