如何在React中获取点击元素的文本内容

huangapple go评论49阅读模式
英文:

How to get text content of a clicked element with React

问题

我有一堆按钮,我想在单击其中任何一个按钮时获取单击元素的 innerText,我该如何在 React 中实现这个功能。

在这个示例中,该代码仅适用于第一个按钮,因为我设置了 value 属性。

function App() {
  const [text, setText] = useState("");

  const handleButtonClick = (e) => {
    console.log(e.target.value);
    setText(e.target.value);
  };

  return (
    <>
      <div className="buttons">
        <button value="buttonOne" onClick={handleButtonClick}>buttonOne</button>
        <button onClick={handleButtonClick}>buttonTwo</button>
      </div>
      <p>{text}</p>
    </>
  )
}
英文:

I have a bunch of buttons and I want when I click on any of them get the innerText of the clicked element how I can do that with React.

In this example the code will work only with first button cause I put the value attribute.

function App() {
  const [text, setText] = useState(&quot;&quot;);

  const handleButtonClick = (e) =&gt; {
    console.log(e.target.value);
    setText(e.target.value);
  };

  return (
    &lt;&gt;
      &lt;div className=&quot;buttons&quot;&gt;
        &lt;button value={&quot;buttonOne&quot;} onClick={handleButtonClick}&gt;buttonOne&lt;/button&gt;
        &lt;button onClick={handleButtonClick}&gt;buttonTwo&lt;/button&gt;
      &lt;/div&gt;
      &lt;p&gt;{text}&lt;/p&gt;
    &lt;/&gt;
  )
}

答案1

得分: 1

你可以使用 element.innerText 来获取按钮的 "文本"。或者,您还可以使用 element.innerHTMLelement.textContent

const { useState, Fragment } = React;

function App() {
  const [text, setText] = useState("");

  const handleButtonClick = (e) => {
    console.log(e.target.innerText);
    setText(e.target.value);
  };

  return (
    <Fragment>
      <div className="buttons">
        <button value={"buttonOne"} onClick={handleButtonClick}>buttonOne</button>
        <button onClick={handleButtonClick}>buttonTwo</button>
      </div>
      <p>{text}</p>
    </Fragment>
  )
}

ReactDOM.createRoot(
    document.getElementById("root")
).render(
    <App />
);

需要注意的是,在React中通常不建议使用这些原生DOM操作,因为对DOM进行命令式操作在声明式应用中是不明智的。更好的方法是使用存储器来存储按钮内容,并从那里检索它。

英文:

You can use element.innerText to get the "text" of the button. Alternatively you can use element.innerHTML or element.textContent.

<!-- begin snippet: js hide: false console: true babel: true -->

<!-- language: lang-js -->

const { useState, Fragment } = React;

function App() {
  const [text, setText] = useState(&quot;&quot;);

  const handleButtonClick = (e) =&gt; {
    console.log(e.target.innerText);
    setText(e.target.value);
  };

  return (
    &lt;Fragment&gt;
      &lt;div className=&quot;buttons&quot;&gt;
        &lt;button value={&quot;buttonOne&quot;} onClick={handleButtonClick}&gt;buttonOne&lt;/button&gt;
        &lt;button onClick={handleButtonClick}&gt;buttonTwo&lt;/button&gt;
      &lt;/div&gt;
      &lt;p&gt;{text}&lt;/p&gt;
    &lt;/Fragment&gt;
  )
}

ReactDOM.createRoot(
    document.getElementById(&quot;root&quot;)
).render(
    &lt;App /&gt;
);

<!-- language: lang-html -->

&lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.development.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

It should be noted that using these native DOM operations is usually an anti-pattern in React, for a declarative app it's ill advised to access content on the DOM imperatively. A better approach would be to use a store for your button contents and retrieve it from there.

huangapple
  • 本文由 发表于 2023年6月8日 19:14:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431253.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定