
按钮点击无响应の原因
在提供的 React 代码中,您使用的是 handleClick 函数作为按钮的 onClick 处理程序。然而,在您的代码中没有定义此函数。这会导致按钮在点击时没有响应。
以下是如何修复此问题:
import React from 'react'
function App5() {
// 定义 handleClick 函数
const handleClick = () => {
console.log(123)
}
return (
<div
style={{ position: 'relative', height: '100vh' }}
>
<div id="header" style={{ width: '100%', height: '24px', backgroundColor: '#ddd',
display: 'flex', justifyContent: 'flex-end'
}}>
{/* Q1: 如何让区域1 在中间呢? */}
<span style={{ margin: '0 auto' }}>区域1</span>
{/* Q2: 没有响应点击事件 */}
<button
onClick={ handleClick }
>开关</button>
</div>
<div id="content" style={{ position: 'absolute', top: '24', bottom: '24px', width: '100%', height: '100%' }}></div>
<div id="footer" style={{ position: 'absolute', bottom: 0, width: '100%', height: '24px', backgroundColor: '#3472BB' }}></div>
</div>
)
}
export default App5
登录后复制
此外,您的代码中还有一些其他问题:
- 在 span 元素中,您没有指定任何样式来使其居中。为了解决这个问题,您可以在 style 属性中添加 margin: 0 auto;。
- 您没有使用 border-radius 属性来设置按钮圆角。如果您希望按钮为圆形,可以添加 border-radius: 50%; 到按钮的 style 属性。
以上就是 React 按钮点击无响应?的详细内容,更多请关注米云其它相关文章!
