【发布时间】:2022-12-04 02:57:54
【问题描述】:
`` 大家好,我想将颜色作为输入,然后根据它更改文本的颜色,但它不起作用任何人都可以帮助我。
import React, {useState} from 'react'
export default function Textform(props) {
//this is function
const newColor =()=>{
const x = document.getElementById("mybox")
let newc =color;
if(x.style.color==="black"){
x.style.color = setcolor(newc)
}
else{
x.style.color = "black"
}
}
const changeColor =(event)=>{
setcolor(event.target.value);
}
const onChange =(event)=>{
setText(event.target.value);
}
const [text, setText] = useState("");
const [color, setcolor] = useState("")
return (
<>
//text area input
<div className="mb-3">
<textarea className="form-control" value={text} onChange={onChange} placeholder="Enter text " name="" id="mybox" rows="8"></textarea>
</div>
//our color choice input
<div className="mb-3">
<textarea className="form-control" value={color} onChange={changeColor} placeholder="Enter your color choice" name="" id="mybox" rows="3"></textarea>
</div>
//this is my button
<button className="btn btn-primary mx-1" onClick={newColor}> Change Color</button>
</>
)
}
我尝试创建一个将文本作为输入的文本区域和另一个将颜色作为输入的文本区域,然后创建一个按钮。当我们按下按钮时,它会根据我们的选择改变文本的颜色。 但是我在实现这个逻辑时出错了。
【问题讨论】:
标签: javascript html reactjs react-functional-component