检查按钮是否处于活动状态,然后获取其值以传递按钮

发布于 2025-01-11 10:32:28 字数 1480 浏览 0 评论 0原文

输入图片此处描述

function Input() {
  const [input, setInput] = useState("");
  function handleSearch() {
    let url = "https://google.com/search?q=${input}"
    window.open(url)
  }
  return (
    <div className="input-wrap">
      <input
        type="text"
        className="input__search"
        placeholder="Enter your search..."
        value={input}
        onChange={(e) => setInput(e.target.value)}></input>
      <button
        className="input__search--btn"
        onClick={handleSearch}>
        <i className="fa-solid fa-magnifying-glass"></i>
      </button>
    </div>
  );
}

单击搜索按钮时,将根据输入字段中的值将您重定向到 google 搜索,下面是高级搜索的站点,激活后,链接将在后面添加一个附加链接“https://google.com/search?q=${input}+site%3A${activepage}.com,如何检查一个或多个网站是否处于活动状态,然后传递其name to url

P/s:切换网站的代码

function WebButton({ icon, name }) {
    const [active, setActive] = useState(false);
    function handleToggle() {
        setActive(!active);
    }
        return (
            <button
            className={active ? "websites-btn active" : "websites-btn"}
            onClick={handleToggle}>
            <i className={icon}></i>
            <div className="websites-name">{name}</div> 
    </button>
  );
}

enter image description here

function Input() {
  const [input, setInput] = useState("");
  function handleSearch() {
    let url = "https://google.com/search?q=${input}"
    window.open(url)
  }
  return (
    <div className="input-wrap">
      <input
        type="text"
        className="input__search"
        placeholder="Enter your search..."
        value={input}
        onChange={(e) => setInput(e.target.value)}></input>
      <button
        className="input__search--btn"
        onClick={handleSearch}>
        <i className="fa-solid fa-magnifying-glass"></i>
      </button>
    </div>
  );
}

The search button when clicked will redirect you to a google search based on the value from the input field, below is the site for advanced search, when active the link will add an additional link after "https://google.com/search?q=${input}+site%3A${activepage}.com, how do I check if one or many sites are active then pass down its name to url

P/s: code for toggling websites

function WebButton({ icon, name }) {
    const [active, setActive] = useState(false);
    function handleToggle() {
        setActive(!active);
    }
        return (
            <button
            className={active ? "websites-btn active" : "websites-btn"}
            onClick={handleToggle}>
            <i className={icon}></i>
            <div className="websites-name">{name}</div> 
    </button>
  );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

走走停停 2025-01-18 10:32:28

您可以保留根级别状态来收集状态的活动链接。并将其传递给 Input 组件。

  1. 更新您的 Input 组件以接受名为 `` 的数组,并更新 handleSearch 以在 Google 搜索中使用 OR 操作。
function Input({ activeLinks }) {
  const [input, setInput] = useState("");
  function handleSearch() {
    if (activeLinks.length > 0) {
      let compundSearchURL = `https://google.com/search?q=${input}`;
      activeLinks.forEach((link, i) => {
        compundSearchURL += `+${i > 0 ? "OR+" : ""}site%3A${link}.com`;
      });
      window.open(compundSearchURL);
    } else {
      window.open(`https://google.com/search?q=${input}`);
    }
  }
  return (
    <div className="input-wrap">
      <input
        type="text"
        className="input__search"
        placeholder="Enter your search..."
        value={input}
        onChange={(e) => setInput(e.target.value)}
      ></input>
      <button className="input__search--btn" onClick={handleSearch}>
        <i className="fa-solid fa-magnifying-glass">Search</i>
      </button>
    </div>
  );
}
  1. 接受 WebButton 中名为 toggleActiveLink 的另一个函数和一个名为 value 的字符串,该字符串引用 URL 部分。使用handleToggle函数内的值调用该函数。
function WebButton({ icon, name, toggleActiveLink, value }) {
  const [active, setActive] = useState(false);
  function handleToggle() {
    setActive(!active);
    toggleActiveLink(value);
  }
  return (
    <button
      className={active ? "websites-btn active" : "websites-btn"}
      style={{ color: active ? "blue" : "unset" }}
      onClick={handleToggle}
    >
      <i className={icon}></i>
      <div className="websites-name">{name}</div>
    </button>
  );
}
  1. 在主要组件中,您必须创建一个本地状态来处理活动链接。根据给定创建切换函数。如果该值不存在,它将添加该值,否则将其删除。
const urls = [
  { name: "Reddit", value: "reddit" },
  { name: "Quora", value: "quara" },
  { name: "Facebook", value: "facebook" },
  { name: "Stackoverflow", value: "stackoverflow" },
  { name: "Twitter", value: "twitter" }
];

function App() {
  const [activeLinks, setActiveLinks] = useState([]);

  const toggleActiveLink = (link) => {
    const index = activeLinks.indexOf(link);
    if (index < 0) {
      setActiveLinks((prevLinks) => [...prevLinks, link]);
    } else {
      setActiveLinks((prevLinks) => prevLinks.filter((l) => l !== link));
    }
  };
  return (
    <>
      <Input activeLinks={activeLinks} />
      <div>
        {urls.map(({ name, value }) => (
          <WebButton
            name={name}
            value={value}
            toggleActiveLink={toggleActiveLink}
          />
        ))}
      </div>
    </>
  );
}

编辑多个网站 google 搜索反应

You can keep a root level state to gather active links to a state. And pass it to the Input component.

  1. Update your Input component to accept array called `` and update the handleSearch to use OR operation in google search.
function Input({ activeLinks }) {
  const [input, setInput] = useState("");
  function handleSearch() {
    if (activeLinks.length > 0) {
      let compundSearchURL = `https://google.com/search?q=${input}`;
      activeLinks.forEach((link, i) => {
        compundSearchURL += `+${i > 0 ? "OR+" : ""}site%3A${link}.com`;
      });
      window.open(compundSearchURL);
    } else {
      window.open(`https://google.com/search?q=${input}`);
    }
  }
  return (
    <div className="input-wrap">
      <input
        type="text"
        className="input__search"
        placeholder="Enter your search..."
        value={input}
        onChange={(e) => setInput(e.target.value)}
      ></input>
      <button className="input__search--btn" onClick={handleSearch}>
        <i className="fa-solid fa-magnifying-glass">Search</i>
      </button>
    </div>
  );
}
  1. Accept another function in WebButton called toggleActiveLink and a string called value which refers to the URL part. Call the function with the value inside handleToggle function.
function WebButton({ icon, name, toggleActiveLink, value }) {
  const [active, setActive] = useState(false);
  function handleToggle() {
    setActive(!active);
    toggleActiveLink(value);
  }
  return (
    <button
      className={active ? "websites-btn active" : "websites-btn"}
      style={{ color: active ? "blue" : "unset" }}
      onClick={handleToggle}
    >
      <i className={icon}></i>
      <div className="websites-name">{name}</div>
    </button>
  );
}
  1. In the main component you have to create a local state to handle the active links. Create the toggle function as given. It will add the value if it is not there otherwise remove it.
const urls = [
  { name: "Reddit", value: "reddit" },
  { name: "Quora", value: "quara" },
  { name: "Facebook", value: "facebook" },
  { name: "Stackoverflow", value: "stackoverflow" },
  { name: "Twitter", value: "twitter" }
];

function App() {
  const [activeLinks, setActiveLinks] = useState([]);

  const toggleActiveLink = (link) => {
    const index = activeLinks.indexOf(link);
    if (index < 0) {
      setActiveLinks((prevLinks) => [...prevLinks, link]);
    } else {
      setActiveLinks((prevLinks) => prevLinks.filter((l) => l !== link));
    }
  };
  return (
    <>
      <Input activeLinks={activeLinks} />
      <div>
        {urls.map(({ name, value }) => (
          <WebButton
            name={name}
            value={value}
            toggleActiveLink={toggleActiveLink}
          />
        ))}
      </div>
    </>
  );
}

Edit mutli site google search react

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文