How to use onClick with divs in React.js(如何在 React.js 中将 onClick 与 div 一起使用)
问题描述
我正在制作一个非常简单的应用程序,您可以在其中单击方形 div 将它们的颜色从白色更改为黑色.但是,我遇到了麻烦.我想使用 onClick 函数来允许用户点击一个正方形来改变它的颜色,但它似乎没有工作.我尝试过使用 spans 和空 p 标签,但这也不起作用.
这里是有问题的代码:
var Box = React.createClass({获取初始状态:函数(){返回 {白颜色'};},改变颜色:函数(){var newColor = this.state.color == 'white' ?'黑,白';这个.setState({颜色:新颜色});},渲染:函数(){返回 (这是我在 CodePen 上的小项目的链接.http://codepen.io/anfperez/pen/RorKge
解决方案 这行得通
var Box = React.createClass({获取初始状态:函数(){返回 {白颜色'};},改变颜色:函数(){var newColor = this.state.color == 'white' ?'黑,白';this.setState({ 颜色: newColor });},渲染:函数(){返回 (在你的css中,删除你拥有的样式并放上这个
.box {宽度:200px;高度:200px;边框:1px纯黑色;向左飘浮;}
您必须设置您正在调用 onClick
的实际 div 的样式.给 div 一个 className,然后设置它的样式.还记得删除你将 App
渲染到 dom 的这个块,App 没有定义
ReactDOM.render(<App/>,document.getElementById('root'));
I'm making a very simple application where you can click on square divs to change their color from white to black. However, I'm having trouble. I'd like to use the onClick function to allow a user to click on a square to change its color, but it doesn't seem to be working. I've tried using spans and empty p tags, but that doesn't work either.
Here is the code in question:
var Box = React.createClass({
getInitialState: function() {
return {
color: 'white'
};
},
changeColor: function() {
var newColor = this.state.color == 'white' ? 'black' : 'white';
this.setState({
color: newColor
});
},
render: function() {
return (
<div>
<div
style = {{background: this.state.color}}
onClick = {this.changeColor}
>
</div>
</div>
);
}
});
Here's a link to my small project on CodePen.
http://codepen.io/anfperez/pen/RorKge
解决方案 This works
var Box = React.createClass({
getInitialState: function() {
return {
color: 'white'
};
},
changeColor: function() {
var newColor = this.state.color == 'white' ? 'black' : 'white';
this.setState({ color: newColor });
},
render: function() {
return (
<div>
<div
className='box'
style={{background:this.state.color}}
onClick={this.changeColor}
>
In here already
</div>
</div>
);
}
});
ReactDOM.render(<Box />, document.getElementById('div1'));
ReactDOM.render(<Box />, document.getElementById('div2'));
ReactDOM.render(<Box />, document.getElementById('div3'));
and in your css, delete the styles you have and put this
.box {
width: 200px;
height: 200px;
border: 1px solid black;
float: left;
}
You have to style the actual div you are calling onClick
on. Give the div a className and then style it. Also remember to remove this block where you are rendering App
into the dom, App is not defined
ReactDOM.render(<App />,document.getElementById('root'));
这篇关于如何在 React.js 中将 onClick 与 div 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何在 React.js 中将 onClick 与 div 一起使用
猜你喜欢
-
失败的 Canvas 360 jquery 插件
2022-01-01
-
addEventListener 在 IE 11 中不起作用
2022-01-01
-
Css:将嵌套元素定位在父元素边界之外一点
2022-09-07
-
Fetch API 如何获取响应体?
2022-01-01
-
Flexslider 箭头未正确显示
2022-01-01
-
CSS媒体查询(最大高度)不起作用,但为什么?
2022-01-01
-
400或500级别的HTTP响应
2022-01-01
-
如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查
2022-01-01
-
Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient()
2022-01-01
-
使用RSelum从网站(报纸档案)中抓取多个网页
2022-09-06