gpt4 book ai didi

javascript - 在没有 Canvas 的情况下获取按钮单击事件

转载 作者:行者123 更新时间:2023-11-30 19:34:42 26 4
gpt4 key购买 nike

如何为按钮和 ListItem 设置 2 个不同的点击事件?
主要对没有 ListItem 事件的按钮点击事件感兴趣。

CodeSandobox DEMO

import React from "react";
import ReactDOM from "react-dom";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";

function App() {
const handleButtonClick = () => {
console.log("button click");
};
const handleListItemClick = () => {
console.log("list click");
};
return (
<ListItem button onClick={() => handleListItemClick()}>
<button onClick={() => handleButtonClick()}>
get only the button click
</button>
<ListItemText primary="without the ListItem click event..." />
</ListItem>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

最佳答案

请检查一下

import React from "react";
import ReactDOM from "react-dom";
import ListItem from "@material-ui/core/ListItem";
import ListItemText from "@material-ui/core/ListItemText";

function App() {
const handleButtonClick = e => {
e.stopPropagation();
console.log("button click");
};
const handleListItemClick = () => {
console.log("list click");
};
return (
<ListItem button onClick={() => handleListItemClick()}>
<button onClick={e => handleButtonClick(e)}>
get only the button click
</button>
<ListItemText primary="without the ListItem click event..." />
</ListItem>
);
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);


关于javascript - 在没有 Canvas 的情况下获取按钮单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56063297/

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