gpt4 book ai didi

javascript - onClick事件不触发reactjs

转载 作者:行者123 更新时间:2023-11-28 19:17:45 29 4
gpt4 key购买 nike

<!DOCTYPE html>
<html>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline;
}
#outer {
margin-left: 20%;
}
#icon {
display: inline;
}
</style>

<head lang="en">
<meta charset="UTF-8">
<title>React Image Slider</title>
<script src="https://fb.me/react-0.13.1.js"></script>
<script src="https://fb.me/JSXTransformer-0.13.1.js"></script>
</head>

<body>
<script type="text/jsx">
var Slider = React.createClass({ changeImage: function(e){ console.log("i m here"); }, render: function () { var lib = this.props.imageData; return
<div>
<div id="outer">
<img src="http://placehold.it/300x300" width="300" height="300" />
</div>
<ul>
{lib.map(function(l){ return
<li>
<div id="icon">
<input type="image" onClick={ this.changeImage} src={l.icon} alt="Submit" width="100" height="100" /> <span>{l.name}</span>
</div>
</li>
}) }
</ul>
</div>; } } );
var arr_img = [ { name: 'Image1', url: 'http://placehold.it/940x528', icon: 'http://placehold.it/100x100'}, { name: 'Image2', url: 'http://placehold.it/940x528', icon: 'http://placehold.it/100x100'}, { name: 'Image3', url: 'http://placehold.it/940x528',
icon: 'http://placehold.it/100x100'}, { name: 'Image4', url: 'http://placehold.it/940x528', icon: 'http://placehold.it/100x100'}, { name: 'Image5', url: 'http://placehold.it/940x528', icon: 'http://placehold.it/100x100'}, { name: 'Image6', url: 'http://placehold.it/940x528',
icon: 'http://placehold.it/100x100'}, { name: 'Image7', url: 'http://placehold.it/940x528', icon: 'http://placehold.it/100x100'}, { name: 'Image8', url: 'http://placehold.it/940x528', icon: 'http://placehold.it/100x100'} ];
React.render(
<Slider imageData={ arr_img} />, document.body );

</script>
</body>

</html>

最佳答案

问题是 this.changeImage 当您传入时将是未定义的,因为您需要设置传递给映射的函数的范围:

// Add `this` as a second parameter
lib.map(function(l){ }, this);

我应该说,这与 React 没有任何关系,而与简单的 JavaScript 有关。

关于javascript - onClick事件不触发reactjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29491948/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com