gpt4 book ai didi

javascript - anchor 标记(标记)onclick 事件处理程序不工作

转载 作者:可可西里 更新时间:2023-11-01 02:38:31 28 4
gpt4 key购买 nike

虽然我在 reactjs 组件(组件名称为 renderLocationLink)的渲染方法返回的 html 中包含了 a 标签的 onclick 处理程序,但渲染正确地发生了 onclick 处理程序属性没有出现在网页上呈现的 html 中。我想要无法确定问题所在,这是代码

var feedApp = React.createClass({
getInitialState: function(){
return {
data : [
{display_name:"Rao",content:"this is Rao post",links:['link1','link2','link3']},
{display_name:"Sultan",content:"this is Sultans",links:['link4','link5','link6']},
{display_name:"John",content:"this is John post",links:['link7','link8','link9']}
]
}
},
fetchFeedsFromUrl: function(){
console.log('Onclick triggered');
},
render: function(){
return (<Feeds data={this.state.data} onClick={this.fetchFeedsFromUrl} />)
}
})

var Feeds = React.createClass({

render: function(){
var onClickfunc = this.props.onClick;
var feeds = this.props.data.map(function(feed){
return (
<oneFeed name={feed.display_name} onClick={this.onClickfunc} content={feed.content} links={feed.links} />
)
});
return(
<div> {feeds} </div>
)
}
})


var oneFeed = React.createClass({
render: function() {
return (
<div>
<h3>{this.props.name}</h3>
<renderLocationLink onClick={this.props.onClick} linkArray={this.props.links} />
<p>{this.props.content} </p>
</div>
)
}
});


var renderLocationLink = React.createClass({

render: function(){
var onClick = this.props.onClick;
var locationLinks = this.props.linkArray.map(function(link,index){
return (<a onClick={this.onClick} href={link}>{link} </a>)
})

return ( <div >{locationLinks}</div> )

}
})

React.renderComponent(feedApp(null),document.body);

最佳答案

您不需要在 map 函数中引用“this”来访问局部变量。当您尝试访问 onClick 变量时删除“this”。

var renderLocationLink = React.createClass({

render: function(){
var onClick = this.props.onClick;
var locationLinks = this.props.linkArray.map(function(link,index){
return (<a onClick={onClick} href={link}>{link} </a>)
})

return ( <div >{locationLinks}</div> )

}
})

关于javascript - anchor 标记(标记)onclick 事件处理程序不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26587754/

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