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

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

How to get text content of a clicked element with React

问题

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

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

  1. function App() {
  2. const [text, setText] = useState("");
  3. const handleButtonClick = (e) => {
  4. console.log(e.target.value);
  5. setText(e.target.value);
  6. };
  7. return (
  8. <>
  9. <div className="buttons">
  10. <button value="buttonOne" onClick={handleButtonClick}>buttonOne</button>
  11. <button onClick={handleButtonClick}>buttonTwo</button>
  12. </div>
  13. <p>{text}</p>
  14. </>
  15. )
  16. }
英文:

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.

  1. function App() {
  2. const [text, setText] = useState(&quot;&quot;);
  3. const handleButtonClick = (e) =&gt; {
  4. console.log(e.target.value);
  5. setText(e.target.value);
  6. };
  7. return (
  8. &lt;&gt;
  9. &lt;div className=&quot;buttons&quot;&gt;
  10. &lt;button value={&quot;buttonOne&quot;} onClick={handleButtonClick}&gt;buttonOne&lt;/button&gt;
  11. &lt;button onClick={handleButtonClick}&gt;buttonTwo&lt;/button&gt;
  12. &lt;/div&gt;
  13. &lt;p&gt;{text}&lt;/p&gt;
  14. &lt;/&gt;
  15. )
  16. }

答案1

得分: 1

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

  1. const { useState, Fragment } = React;
  2. function App() {
  3. const [text, setText] = useState("");
  4. const handleButtonClick = (e) => {
  5. console.log(e.target.innerText);
  6. setText(e.target.value);
  7. };
  8. return (
  9. <Fragment>
  10. <div className="buttons">
  11. <button value={"buttonOne"} onClick={handleButtonClick}>buttonOne</button>
  12. <button onClick={handleButtonClick}>buttonTwo</button>
  13. </div>
  14. <p>{text}</p>
  15. </Fragment>
  16. )
  17. }
  18. ReactDOM.createRoot(
  19. document.getElementById("root")
  20. ).render(
  21. <App />
  22. );

需要注意的是,在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 -->

  1. const { useState, Fragment } = React;
  2. function App() {
  3. const [text, setText] = useState(&quot;&quot;);
  4. const handleButtonClick = (e) =&gt; {
  5. console.log(e.target.innerText);
  6. setText(e.target.value);
  7. };
  8. return (
  9. &lt;Fragment&gt;
  10. &lt;div className=&quot;buttons&quot;&gt;
  11. &lt;button value={&quot;buttonOne&quot;} onClick={handleButtonClick}&gt;buttonOne&lt;/button&gt;
  12. &lt;button onClick={handleButtonClick}&gt;buttonTwo&lt;/button&gt;
  13. &lt;/div&gt;
  14. &lt;p&gt;{text}&lt;/p&gt;
  15. &lt;/Fragment&gt;
  16. )
  17. }
  18. ReactDOM.createRoot(
  19. document.getElementById(&quot;root&quot;)
  20. ).render(
  21. &lt;App /&gt;
  22. );

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

  1. &lt;div id=&quot;root&quot;&gt;&lt;/div&gt;
  2. &lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.development.js&quot;&gt;&lt;/script&gt;
  3. &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:

确定