Javascript 使react组件可单击
我有一个功能性的REACT组件,代码如下Javascript 使react组件可单击,javascript,reactjs,react-bootstrap,Javascript,Reactjs,React Bootstrap,我有一个功能性的REACT组件,代码如下 const MobileListing = (props) => { function handleClick() { console.log('in cardClick'); } return ( <div> <Row> <Card onClick={() => handleClick()} st
const MobileListing = (props) => {
function handleClick() {
console.log('in cardClick');
}
return (
<div>
<Row>
<Card onClick={() => handleClick()} style={{cursor : 'pointer'}} >
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the bulk of
the card's content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</Row>
</div>
);
}
export default MobileListing;
const MobileListing=(道具)=>{
函数handleClick(){
console.log('in cardClick');
}
返回(
handleClick()}style={{cursor:'pointer'}}>
卡片标题
一些快速的示例文本构建在卡片标题上,并构成大部分
卡片的内容。
去某处
);
}
导出默认移动列表;
我想让整个卡片都可以点击。我通读了一篇关于堆栈溢出的帖子,其中谈到了使用锚定标记,但这对我不起作用。
有人能帮我理解我做错了什么吗
在我的网站上有一张这样的卡片,我想让整个卡片都可以点击。
您可以在顶级
div
元素上使用onClick
,或者,如果行中有更多卡片,您可以用div
包装每个卡片,并为其赋予onClick
属性
例如:
<div>
<Row>
<div onClick={handleClick}>
<Card style={{ width: '18rem', cursor : 'pointer' }} >
<Card.Img variant="top" src="holder.js/100px180" />
<Card.Body>
<Card.Title>Card Title</Card.Title>
<Card.Text>
Some quick example text to build on the card title and make up the bulk of
the card's content.
</Card.Text>
<Button variant="primary">Go somewhere</Button>
</Card.Body>
</Card>
</div>
</Row>
</div>
卡片标题
一些快速的示例文本构建在卡片标题上,并构成大部分
卡片的内容。
去某处
最好用锚定标签将其包裹起来,因为可点击的东西应该是指向某个地方的链接,如果可以点击,应该是按钮。
将单击处理程序与其他内容一起使用只会使内容无法访问请解释当前实现的行为。@tushar My current component是一个子组件,用于在审阅网站上格式化列表。所以我将一些数据传递给这个组件,它将一个格式化的组件返回给父组件。好的@Vik G。现在,卡片组件的哪些元素是可点击的?现在卡片上没有任何东西是可点击的,我正在尝试使整张卡片可点击的。可以使用顶级div代替onClick
,如果这对样式有任何影响,请使用另一个div
将内部Card
组件包装起来,并给它一个属性onClick
。这应该是一个注释