reactjs 更改下拉标题的字体颜色

eqoofvh9  于 2023-03-17  发布在  React
关注(0)|答案(1)|浏览(225)

我正在使用react-bootstrap**NavBar组件,其中我正在使用NavDropdown**,它只是导航栏中的一个下拉列表。
在我的页面中,我有2个下拉菜单,如图所示。

我想要的是,如果我从Dropdown 2中选择任何值,则下拉标题Dropdown2的字体颜色将从白色变为黄色,而Dropdown 1保持不变。
类似地,如果我从Dropdown1中选择任何值,同样的事情会发生。

如何使用CSS实现这一点?
查找实时工作代码here

这是密码

import { Stack, Form, Button, Alert, Nav, Navbar, Container, NavDropdown, Offcanvas } from "react-bootstrap"
import { useState, useEffect, useRef } from 'react'

export default function App() {

  const [val, setVal] = useState("");

  return <div style={{ padding: "15px" }}>

    <h1>Example</h1>

    <Navbar bg="dark" className="my-2" variant="dark">
      <Container>
        <Navbar.Brand href="#home">Navbar</Navbar.Brand>
        <Nav className="me-auto">
          <Nav.Link href="#home">Home</Nav.Link>
          <NavDropdown
            title="Dropdown1"
            id={`offcanvasNavbarDropdown-expand-sm}`}
          >
            <NavDropdown.Item onClick={() => setVal("1_1 Small")} >1_1 Small</NavDropdown.Item>
            <NavDropdown.Item onClick={() => setVal("1_1 Medium")}>1_2 Medium</NavDropdown.Item>
            <NavDropdown.Item onClick={() => setVal("1_1 Large")}>1_3 Large</NavDropdown.Item>

          </NavDropdown>

          <NavDropdown
            title="Dropdown2"
            id={`offcanvasNavbarDropdown-expand-sm}`}
          >
            <NavDropdown.Item onClick={()=>setVal("2_1 Small")}>2_1 Small</NavDropdown.Item>
            <NavDropdown.Item onClick={() => setVal("2_2 Medium")}>2_2 Medium</NavDropdown.Item>
            <NavDropdown.Item onClick={() => setVal("2_3 Large")}>2_3 Large</NavDropdown.Item>
            <NavDropdown.Item onClick={() => setVal("2_4 ExtraLarge")}>2_4 Extra Large</NavDropdown.Item>

          </NavDropdown>
        </Nav>

        
      </Container>
    </Navbar>

   <div className="p-2 mx-auto">
          <h3>Value Selected : {val}</h3>
        </div>
    <br />

  </div>
}
9fkzdhlc

9fkzdhlc1#

这里是你可以抓住这一点。我已经重写了你的应用程序组件,并添加了一个className={selectedDropdown === "Dropdown2" ? "selected" : ""}下拉和设置为下拉状态!

import { Stack, Form, Button, Alert, Nav, Navbar, Container, NavDropdown, Offcanvas } from "react-bootstrap"
import { useState, useEffect, useRef } from 'react'

export default function App() {

  const [val, setVal] = useState("");
 // here adding a new state to keep track of which dropdown is selected
  const [selectedDropdown, setSelectedDropdown] = useState(""); 

  return (
    <div style={{ padding: "15px" }}>

      <h1>Example</h1>

      <Navbar bg="dark" className="my-2" variant="dark">
        <Container>
          <Navbar.Brand href="#home">Navbar</Navbar.Brand>
          <Nav className="me-auto">
            <Nav.Link href="#home">Home</Nav.Link>
            <NavDropdown
              title="Dropdown1"
              id={`offcanvasNavbarDropdown-expand-sm}`}
              className={selectedDropdown === "Dropdown1" ? "selected" : ""} 
              onSelect={() => setSelectedDropdown("Dropdown1")} 
            >
              <NavDropdown.Item onClick={() => setVal("1_1 Small")}>1_1 Small</NavDropdown.Item>
              <NavDropdown.Item onClick={() => setVal("1_1 Medium")}>1_2 Medium</NavDropdown.Item>
              <NavDropdown.Item onClick={() => setVal("1_1 Large")}>1_3 Large</NavDropdown.Item>

            </NavDropdown>

            <NavDropdown
              title="Dropdown2"
              id={`offcanvasNavbarDropdown-expand-sm}`}
              className={selectedDropdown === "Dropdown2" ? "selected" : ""} 
              onSelect={() => setSelectedDropdown("Dropdown2")} 
            >
              <NavDropdown.Item onClick={() => setVal("2_1 Small")}>2_1 Small</NavDropdown.Item>
              <NavDropdown.Item onClick={() => setVal("2_2 Medium")}>2_2 Medium</NavDropdown.Item>
              <NavDropdown.Item onClick={() => setVal("2_3 Large")}>2_3 Large</NavDropdown.Item>
              <NavDropdown.Item onClick={() => setVal("2_4 ExtraLarge")}>2_4 Extra Large</NavDropdown.Item>
            </NavDropdown>
          </Nav>

        </Container>
      </Navbar>

      <div className="p-2 mx-auto">
        <h3>Value Selected : {val}</h3>
      </div>

      <br />
    </div>
  )
}

现在你可以添加到你的CSS和检查它应该工作,只改变颜色为黄色。

.selected > .nav-link {
  color: yellow !important;
}

如果这不起作用,请告诉我!

相关问题