登录
首页 >  文章 >  前端

为什么 React 按钮点击无响应?

时间:2024-11-18 11:49:17 313浏览 收藏

推广推荐
免费电影APP ➜
支持 PC / 移动端,安全直达

大家好,今天本人给大家带来文章《为什么 React 按钮点击无响应? 》,文中内容主要涉及到,如果你对文章方面的知识点感兴趣,那就请各位朋友继续看下去吧~希望能真正帮到你们,谢谢!

为什么 React 按钮点击无响应?

按钮点击无响应の原因

在提供的 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 属性。

今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>